Presentation is loading. Please wait.

Presentation is loading. Please wait.

명품 JAVA Essential.

Similar presentations


Presentation on theme: "명품 JAVA Essential."— Presentation transcript:

1 명품 JAVA Essential

2 학습 목표 자바의 입출력 스트림에 대한 이해 텍스트 파일 입출력 바이너리 파일 입출력 File 클래스로 파일 속성 알아내기
파일 복사 응용 사례

3 자바의 입출력 스트림 자바의 입출력 스트림 입출력 장치와 자바 응용 프로그램 연결 특징
입력 스트림 : 입력 장치로부터 자바 프로그램으로 데이터를 전달하는 객체 출력 스트림 : 자바 프로그램에서 출력 장치로 데이터를 보내는 객체 특징 입출력 스트림 기본 단위 : 바이트 단방향 스트림, 선입선출 구조 자바 프로그램 개발자는 직접 입력 장치에서 읽지 않고 입력 스트림을 통해 읽으며, 스크린 등 출력 장치에 직접 출력하지 않고 출력 스트림에 출력하면 된다.

4 자바의 입출력 스트림 종류 문자 스트림 바이트 스트림 문자만 입출력하는 스트림
문자가 아닌 바이너리 데이터는 스트림에서 처리하지 못함 문자가 아닌 데이터를 문자 스트림으로 출력하면 깨진 기호가 출력 바이너리 파일을 문자 스트림으로 읽으면 읽을 수 없는 바이트가 생 겨서 오류 발생 예) 텍스트 파일을 읽는 입력 스트림 바이트 스트림 입출력 데이터를 단순 바이트의 흐름으로 처리 문자 데이터 든 바이너리 데이터든 상관없이 처리 가능 예) 바이너리 파일을 읽는 입력 스트림

5 문자 스트림과 바이트 스트림의 흐름 비교

6 JDK의 바이트 스트림 클래스 계층 구조 클래스 이름이 공통적으로 Stream으로 끝남

7 JDK의 문자 스트림 클래스 계층 구조 클래스 이름이 공통적으로 Reader/Writer로 끝남

8 스트림 연결 여러 개의 스트림을 연결하여 사용할 수 있음 예) 키보드에서 문자를 입력받기 위해 System.in과
InputStreamReader를 연결한 코드 InputStreamReader rd = new InputStreamReader(System.in); while(true) { int c = rd.read(); // 입력 스트림으로부터 키 입력. c는 입력된 키 문자 값 if(c == -1) // 입력 스트림의 끝을 만나는 경우 break; // 입력 종료 }

9 문자 스트림으로 텍스트 파일 읽기 텍스트 파일을 읽기 위해 문자 스트림 FileReader 클래스 이용
1. 파일 입력 스트림 생성(파일 열기) 스트림을 생성하고 파일을 열어 스트림과 연결 2. 파일 읽기 read()로 문자 하나 씩 파일에서 읽음 3. 스트림 닫기 스트림이 더 이상 필요 없으면 닫아야 함. 닫힌 스트림에서는 읽을 수 없음 close()로 스트림 닫기 FileReader fin = new FileReader("c:\\test.txt"); int c; while((c = fin.read()) != -1) { // 문자를 c에 읽음. 파일 끝까지 반복 System.out.print((char)c); // 문자 c 화면에 출력 } fin.close();

10 파일 입출력과 예외 처리 파일 입출력 동안 예외 발생 가능 try-catch 블록 반드시 필요
스트림 생성 동안 : FileNotFoundException 발생 가능 파일의 경로명이 틀리거나, 디스크의 고장 등으로 파일을 열 수 없음 파일 읽기, 쓰기, 닫기를 하는 동안 : IOException 발생 가능 디스크 오동작, 파일이 중간에 깨진 경우, 디스크 공간이 모자라서 파일 입출력 불가 try-catch 블록 반드시 필요 자바 컴파일러의 강제 사항 FileReader fin = new FileReader("c:\\test.txt"); // FileNotFoundException 발생가능 int c = fin.read(); // IOException 발생 가능 try { FileReader fin = new FileReader("c:\\test.txt"); .. int c = fin.read(); ... fin.close(); } catch(FileNotFoundException e) { System.out.println("파일을 열 수 없음"); } catch(IOException e) { System.out.println("입출력 오류"); } 생략 가능. FileNotFoundException은 IOException을 상속받기 때문에 아래의 catch 블록 하나만 있으면 됨

11 FileReader의 생성자와 주요 메소드

12 예제 13-1 : FileReader로 텍스트 파일 읽기
FileReader를 이용하여 c:\windows\system.ini 파일을 읽어 화면에 출력하는 프로그램을 작성하라. system.ini는 텍스트 파일이다.. import java.io.*; public class FileReaderEx { public static void main(String[] args) { FileReader in = null; try { in = new FileReader("c:\\windows\\system.ini"); int c; while ((c = in.read()) != -1) { // 한 문자씩 파일 끝까지 읽는다. System.out.print((char)c); } in.close(); catch (IOException e) { System.out.println("입출력 오류"); ; for 16-bit app support [386Enh] woafont=dosapp.fon EGA80WOA.FON=EGA80WOA.FON EGA40WOA.FON=EGA40WOA.FON CGA80WOA.FON=CGA80WOA.FON CGA40WOA.FON=CGA40WOA.FON [drivers] wave=mmdrv.dll timer=timer.drv [mci] 파일 끝을 만나면 -1 리턴

13 문자 스트림으로 텍스트 파일 쓰기 텍스트 파일에 쓰기 위해 문자 스트림 FileWriter 클래스 이용
1. 파일 출력 스트림 생성(파일 열기) 스트림을 생성하고 파일을 열어 스트림과 연결 2. 파일 쓰기 write()로 문자 하나 씩 파일에 기록 블록 단위로 쓰기 가능 3. 스트림 닫기 close()로 스트림 닫기 FileWriter fout = new FileWriter("c:\\test.txt"); fout.write('A'); // 문자 'A'를 파일에 기록 char [] buf = new char [1024]; fout.write(buf, 0, buf.length); // buf[0]부터 버퍼 크기만큼 쓰기 fout.close(); // 스트림 닫기. 더 이상 스트림에 기록할 수 없다.

14 FileWriter의 생성자와 주요 메소드

15 예제 13-2 : FileWriter를 이용하여 텍스트 파일 쓰기
사용자로부터 입력받은 텍스트를 c:\tmp\test.txt 파일에 저장하는 프로그램을 작성하라. 사용자는 키 입력 후 라인 첫 위치에 ctrl-z 키(EOF)를 입력하라. import java.io.*; public class FileWriterEx { public static void main(String[] args) { InputStreamReader in = new InputStreamReader(System.in); FileWriter fout = null; int c; try { fout = new FileWriter("c:\\tmp\\test.txt"); while ((c = in.read()) != -1) { fout.write(c); // 키보드로부터 받은 문자를 파일에 저장 } in.close(); fout.close(); catch (IOException e) { System.out.println("입출력 오류"); <Enter> 키 ctrl-z 키 입력 실행 결과 test.txt 파일 생성

16 바이트 스트림으로 바이너리 파일 쓰기 바이너리 값을 파일에 저장하기
프로그램 내의 변수, 배열, 버퍼에 든 바이너리 값을 파일에 그대로 기록 FileOutputStream 클래스 이용 1. 파일 출력 스트림 생성(파일 열기) 스트림을 생성하고 파일을 열어 스트림과 연결 2. 파일 쓰기 write()로 문자 하나 씩 파일에 기록 3. 스트림 닫기 close()로 스트림 닫기 FileOutputStream fout = new FileOutputStream("c:\\test.out"); byte b[] = {7,51,3,4,-1,24}; for(int i=0; i<b.length; i++) fout.write(b[i]); // 배열 b를 바이너리 그대로 기록 test.out 파일 내부

17 FileOutputStream의 생성자와 주요 메소드

18 예제 13-3 : FileOutputStream으로 바이너리 파일 쓰기
FileOutputStream을 이용하여 byte [] 배열 속에 들어 있는 바이너리 값을 c:\test.out 파일에 저장하라. 이 파일은 바이너리 파일이 된다. 이 파일은 예제 13-4에서 읽어 출력할 것이다. import java.io.*; public class FileOutputStreamEx { public static void main(String[] args) { byte b[] = {7,51,3,4,-1,24}; try { FileOutputStream fout = new FileOutputStream("c:\\test.out"); for(int i=0; i<b.length; i++) fout.write(b[i]); // 배열 b의 바이너리를 그대로 기록 fout.close(); } catch(IOException e) { } System.out.println("c:\\test.out을 저장하였습니다."); } fout.write(b); 한 줄로 코딩 가능 c:\test.out을 저장하였습니다. test.out 파일 내부

19 바이트 스트림으로 바이너리 파일 읽기 바이너리 파일에서 읽기 위해 FileInputStream 클래스 이용
1. 파일 입력 스트림 생성(파일 열기) 스트림을 생성하고 파일을 열어 스트림과 연결 2. 파일 읽기 read()로 문자 하나 씩 파일에서 읽기 블록 단위로 읽기 가능 3. 스트림 닫기 close()로 스트림 닫기 FileInputStream fin = new FileInputStream("c:\\test.out"); int n=0, c; while((c = fin.read()) != -1) { b[n] = (byte)c; // 읽은 바이트를 배열에 저장 n++; } fin.read(b); // 배열 b의 바이트 크기만큼 바이너리 그대로 읽기

20 FileInputStream의 생성자와 주요 메소드

21 예제 13-4 : FileInputStream으로 바이너리 파일 읽기
FileInputStream을 이용하여 c:\test.out 파일(예제 13-3에서 저장한 파일)을 읽어 바이너리 값들을 byte [] 배열 속에 저장하고 화면에 출력하라. import java.io.*; public class FileInputStreamEx { public static void main(String[] args) { byte b[] = new byte [6]; // 비어 있는 byte 배열 try { FileInputStream fin = new FileInputStream("c:\\test.out"); int n=0, c; while((c = fin.read())!= -1) { b[n] = (byte)c; // 읽은 바이트를 배열에 저장 n++; } System.out.println("c:\\test.out에서 읽은 배열을 출력합니다."); for(int i=0; i<b.length; i++) System.out.print(b[i]+" "); System.out.println(); fin.close(); } catch(IOException e) { } test.out 파일 내부 c:\test.out에서 읽은 배열을 출력합니다.

22 File 클래스 File 클래스 File 객체 생성 파일의 경로명 및 속성을 다루는 클래스
java.io.File 파일과 디렉터리 경로명의 추상적 표현 파일 이름 변경, 삭제, 디렉터리 생성, 크기 등 파일 관리 File 객체에는 파일 읽기/쓰기 기능 없음 파일 입출력은 파일 입출력 스트림 이용 File 객체 생성 생성자에 파일 경로명을 주어 File 객체 생성 디렉터리와 파일명을 나누어 생성자 호출 File f = new File("c:\\tmp\\test.txt"); File f = new File("c:\\tmp", "test.txt");

23 File 클래스 생성자와 주요 메소드

24 File 클래스 활용 파일 크기 파일 경로명 파일 타입 디렉터리 파일 리스트 얻기 long size = f.length();
File f = new File("c:\\windows\\system.ini"); String filename = f.getName(); // "system.ini" String path = f.getPath(); // "c:\\windows\\system.ini" String parent = f.getParent(); // "c:\\windows" 파일 타입 if(f.isFile()) System.out.println(f.getPath() + "는 파일입니다."); // 파일 else if(f.isDirectory()) System.out.println(f.getPath() + "는 디렉터리입니다."); // 디렉터리 c:\windows\system.ini은 파일입니다. 디렉터리 파일 리스트 얻기 File f = new File("c:\\tmp"); File[] subfiles = f.listFiles(); // c:\tmp의 파일 및 서브 디렉터리 리스트 얻기 for(int i=0; i<filenames.length; i++) { System.out.print(subfiles[i].getName()); // 서브 파일명 출력 System.out.println("\t파일 크기: " + subfiles[i].length()); //서브파일크기출력 }

25 예제 13-5 : File 클래스를 활용한 파일 관리 File 클래스를 이용하여, 파일 타입 및 경로명 알아내기, 디렉터리 생성, 파일 이름 변경, 디렉터리의파일 리스트 출력 등 다양한 파일 관리 사례를 보여준다.. import java.io.File; public class FileClassExample { public static void listDirectory(File dir) { System.out.println("-----" + dir.getPath() + "의 서브 리스트 입니다.-----"); File[] subFiles = dir.listFiles(); for(int i=0; i<subFiles.length; i++) { File f = subFiles[i]; long t = f.lastModified(); // 마지막으로 수정된 시간 System.out.print(f.getName()); System.out.print("\t파일 크기: " + f.length()); // 파일 크기 System.out.printf("\t수정한 시간: %tb %td %ta %tT\n",t, t, t, t); } public static void main(String[] args) { File f1 = new File("c:\\windows\\system.ini"); System.out.println( f1.getPath() + ", " + f1.getParent() + ", " + f1.getName()); String res=""; if(f1.isFile()) res = "파일"; else if(f1.isDirectory()) res = "디렉토리"; System.out.println(f1.getPath() + "은 " + res + "입니다."); File f2 = new File("c:\\tmp\\java_sample"); if(!f2.exists()) { f2.mkdir(); } listDirectory(new File("c:\\tmp")); f2.renameTo(new File("c:\\tmp\\javasample")); listDirectory(new File("c:\\tmp")); c:\windows\system.ini, c:\windows, system.ini c:\windows\system.ini은 파일입니다. -----c:\tmp의 서브 리스트 입니다.----- java_sample 파일 크기: 0 수정한 시간: 4월 28 월 18:31:02 song.txt 파일 크기: 20 수정한 시간: 5월 30 수 15:09:33 student.txt 파일 크기: 수정한 시간: 7월 08 월 21:46:47 tellephone.txt 파일 크기: 14 수정한 시간: 5월 13 월 12:38:11 wtc2-02f.mid 파일 크기: 수정한 시간: 4월 01 금 05:18:54 javasample 파일 크기: 0 수정한 시간: 4월 28 월 18:31:02

26 예제 13-6 : 텍스트 파일 복사 문자 스트림 FileReader와 FileWriter를 이용하여 c:\windows\system.ini를 c:\tmp\system.txt 파일로 복사하는 프로그램을 작성하라. import java.io.*; public class TextCopy { public static void main(String[] args){ File src = new File("c:\\windows\\system.ini"); // 원본 파일 경로명 File dest = new File("c:\\tmp\\system.txt"); // 복사 파일 경로명 int c; try { FileReader fr = new FileReader(src); // 파일 입력 문자 스트림 생성 FileWriter fw = new FileWriter(dest); // 파일 출력 문자 스트림 생성 while((c = fr.read()) != -1) { // 문자 하나 읽고 fw.write((char)c); // 문자 하나 쓰고 } fr.close(); fw.close(); System.out.println( src.getPath()+ "를 " + dest.getPath()+ "로 복사하였습니다."); } catch (IOException e) { System.out.println("파일 복사 오류"); c:\windows\system.ini를 c:\tmp\system.txt로 복사하였습니다.

27 예제 13-7 : 바이너리 파일 복사 바이트 스트림 FileInputStream과 FileOutputStream을 이용하여 이미지 파일을 복사하라. import java.io.*; public class BinaryCopy { public static void main(String[] args) { File src = new File( "c:\\Users\\Public\\Pictures\\Sample Pictures\\desert.jpg"); File dest = new File("c:\\tmp\\desert.jpg"); int c; try { FileInputStream fi = new FileInputStream(src); FileOutputStream fo = new FileOutputStream(dest); while((c = fi.read()) != -1) { fo.write((byte)c); } fi.close(); fo.close(); System.out.println( src.getPath()+ "를 " + dest.getPath()+ "로 복사하였습니다."); } catch (IOException e) { System.out.println("파일 복사 오류"); c:\Users\Public\Pictures\Sample Pictures\desert.jpg를 c:\tmp\desert.jpg로 복사하였습니다.

28 예제 13-8 : 고속 복사를 위한 블록 단위 바이너리 파일 복사
예제 13-7을 10KB씩 읽고 쓰도록 수정하여 고속으로 파일을 복사하라. import java.io.*; public class BlockBonaryCopy { public static void main(String[] args) { File src = new File( "c:\\Users\\Public\\Pictures\\Sample Pictures\\desert.jpg"); // 원본 파일 File dest = new File("c:\\tmp\\desert.jpg"); // 복사 파일 try { FileInputStream fi = new FileInputStream(src); // 파일 입력 바이트 스트림 생성 FileOutputStream fo = new FileOutputStream(dest); // 파일 출력 바이트 스트림 생성 byte [] buf = new byte [1024*10]; // 10KB 버퍼 while(true) { int n = fi.read(buf); // 버퍼 크기만큼 읽기. n은 실제 읽은 바이트 fo.write(buf, 0, n); // buf[0]부터 n 바이트 쓰기 if(n < buf.length) break; // 버퍼 크기보다 작게 읽었기 때문에 파일 끝에 도달. 복사 종료 } fi.close(); fo.close(); System.out.println(src.getPath() + "를 " + dest.getPath() + "로 복사하였습니다."); } catch (IOException e) { System.out.println("파일 복사 오류"); } c:\Users\Public\Pictures\Sample Pictures\desert.jpg를 c:\tmp\desert.jpg로 복사하였습니다.


Download ppt "명품 JAVA Essential."

Similar presentations


Ads by Google