prac 8_1 FileWriter
import java.io.*;
import java.util.*;
public class Practice {
public static void main(String[] args) {
FileWriter fw = null; //정보를 입력받아 파일에 저장하는 filewriter
File f = new File("C:\\temp\\phone.txt"); // 파일 클래스로
try {
fw = new FileWriter(f);
Scanner scanner = new Scanner(System.in);
System.out.println("전화번호 입력 프로그램입니다.");
while (true) {
System.out.print("이름 전화번호 >> ");
String line = scanner.nextLine();
if (line.equals("그만"))
break;
fw.write(line + "\r\n"); //fw 파일에 입력받은 정보 한 줄씩 띄기
}
System.out.println(f.getPath() + "에 저장하였습니다.");
scanner.close();
fw.close();
} catch (IOException e) {
System.out.println("입출력 오류");
}
}
}
prac 8_2 FileReader
import java.io.*;
public class Practice {
public static void main(String[] args) {
FileReader fin = null; //FileReader
File f = new File("C:\\temp\\phone.txt"); // 파일 클래스로
System.out.println(f.getPath() + "를 출력합니다.");
try {
fin = new FileReader(f);
int c;
while ( (c = fin.read()) != -1) { //파일의 끝까지 읽어들이고 출력
System.out.print( (char) c);
}
fin.close();
} catch (IOException e) {
System.out.println("입출력 오류");
}
}
}
prac 8_3 FileReader 대문자 출력
import java.io.*;
public class Practice {
public static void main(String[] args) {
FileReader fin = null; //FileReader
File f = new File("C:\\windows\\system.ini"); // 파일 클래스로
try {
fin = new FileReader(f);
int c;
while ( (c = fin.read()) != -1) { //파일의 끝까지 읽어들이고 출력
char a = (char) c; // c를 대문자로 출력하기 위한 문자 a = c 선언
a = Character.toUpperCase(a); //a 대문자로 출력
System.out.print(a); //대문자로 바뀐 a=system.ini 파일 출력
}
fin.close();
} catch (IOException e) {
System.out.println("입출력 오류");
}
}
prac 8_4 FileReader printf
import java.io.*;
import java.util.*;
public class Practice {
public static void main(String[] args) {
FileReader fin = null; // FileReader
File f = new File("C:\\windows\\system.ini"); // 파일 클래스로
try {
fin = new FileReader(f);
Scanner scanner = new Scanner(fin);
System.out.println(f.getPath() + " 파일을 읽어 출력합니다.");
int c = 0;
while (scanner.hasNext()) { // 파일의 끝까지 읽어들이고 출력
c++;
String line = scanner.nextLine();
System.out.printf("%4d" + ": ", c);
System.out.println(line);
}
} catch (IOException e) {
System.out.println("입출력 오류");
}
}
}
prac 8_5 FileInputStream fin fout
import java.io.*;
import java.util.*;
public class Practice {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
FileInputStream fin = null;
FileInputStream fout = null;
System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 파일에 있어야 합니다.");
System.out.print("첫번째 파일 이름을 입력하세요>> ");
String s = scanner.nextLine();
System.out.print("두번째 파일 이름을 입력하세요>> ");
String d = scanner.nextLine();
System.out.println(s + "와 " + d + "를 비교합니다.");
try {
fin = new FileInputStream(s);
fout = new FileInputStream(d);
if(compareFile(fin, fout))
System.out.println("파일이 같습니다.");
else
System.out.println("파일이 다릅니다.");
if (fin != null) fin.close();
if (fout != null) fout.close();
} catch (IOException e) {
System.out.println("파일 입력 오류");
}
scanner.close();
}
private static boolean compareFile(FileInputStream fin, FileInputStream fout) throws IOException {
byte[] finBuf = new byte[1024];
byte[] foutbuf = new byte[1024];
int finCount = 0, foutCount;
while(true) {
finCount = fin.read(finBuf, 0, finBuf.length);
foutCount = fout.read(foutbuf, 0, foutbuf.length);
if (finCount != foutCount)
return false;
if (finCount == -1)
break;
for(int i = 0; i<finCount; i++) {
if (finBuf[i] != foutbuf[i] )
return false;
}
}
return true;
}
prac 8_6 FileReader FileWriter
import java.io.*;
import java.util.*;
public class Practice {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
FileReader fr = null;
FileWriter fw = null;
System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 파일에 있어야 합니다.");
System.out.print("첫번째 파일 이름을 입력하세요>> ");
String s = scanner.nextLine();
System.out.print("두번째 파일 이름을 입력하세요>> ");
String d = scanner.nextLine();
try {
fr = new FileReader(s);
fw = new FileWriter("appended.txt");
int c;
while ((c = fr.read()) != -1) {
fw.write((char) c);
}
fr = new FileReader(d);
while ((c = fr.read()) != -1) {
fw.write((char) c);
}
if (fr != null) fr.close();
fw.close();
System.out.println("프로젝트 폴더 밑에 appended.txt 파일에 저장하였습니다.");
} catch (IOException e) {
System.out.println("파일 입력 오류");
}
scanner.close();
}
}
prac 8_7 File 클래스 / FileInputStream FileOutputStream / byte[] buf
import java.io.*;
import java.util.*;
public class Practice {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
File src = new File("a.jpg");
File dst = new File("b.jpg");
try {
FileInputStream fin = new FileInputStream(src);
FileOutputStream fout = new FileOutputStream(dst);
System.out.println(src.getPath() + "를 " + dst.getPath() + "로 복사합니다.");
System.out.println("10%마다 *를 출력합니다.");
long t = ((src.length()) / 10);
byte[] buf = new byte[(int) t];
while (true) {
int n = fin.read(buf);
fout.write(buf, 0, n);
if (n < buf.length)
break;
System.out.print("*");
}
fin.close();
fout.close();
} catch (IOException e) {
System.out.println("파일 입력 오류");
}
scanner.close();
}
}
prac 8_8 File[] sub = src.listFiles
import java.io*;
public class Practice {
public static void main(String[] args) {
File src = new File("C:\\");
File[] sub = src.listFiles();
File max = null;
for(int i = 0; i < sub.length; i++) {
if( i == 0)
max = sub[i];
if(max.length() < sub[i].length())
max = sub[i];
}
System.out.print("가장 큰 파일은 " + max.getPath() + " " + max.length() + "바이트");
}
}
prac 8_9 File sub.listFiles()
import java.io.*;
public class Practice {
public static void main(String[] args) {
File dir = new File("C:\\temp\\");
File [] sub = dir.listFiles();
System.out.println(dir.getPath() + "디렉터리의 .txt 파일을 모두 삭제합니다.");
int c = 0;
for(int i = 0; i<sub.length; i++) { //파일리스트 sub 전체를 읽으면서
if (!sub[i].isFile()) //파일이 아니면 다음으로, txt 파일을 찾기 위해
continue;
//파일 삭제 메시지 출력을 위해 파일 경로 불러오는 name 선언
String name = sub[i].getName();
int index = name.lastIndexOf(".txt");
if (index == -1) {
System.out.println(name + " 삭제");
sub[i].delete();
c++;
}
}
System.out.println("총 " + c + "개의 .txt파일을 삭제하였습니다.");
}
}
prac 8_10 HashMap FileReader
import java.io.*;
import java.util.*;
public class Practice {
public static void main(String[] args) {
HashMap<String, String> h = new HashMap<>();
Scanner scanner;
try {
scanner = new Scanner(new FileReader("C:\\temp\\phone.txt"));
while(scanner.hasNext()) {
String[] arr = scanner.nextLine().split(" ");
h.put(arr[0], arr[1]);
}
scanner = new Scanner(System.in);
System.out.printf("총 %d개의 전화번호를 읽었습니다.", h.size());
while(true) {
System.out.print("이름>> ");
String str = scanner.next();
if(str.equals("그만"))
break;
String num = h.get(str);
if(num == null)
System.out.println("찾는 이름이 없습니다.");
else
System.out.println(num);
}
scanner.close();
} catch(IOException e) {
System.out.print("파일 입력 오류");
}
}
}
prac 8_11