java.io 패키지 실습 - 파일 읽기,쓰기 (2차 스트림)-BufferedInputStream-
파일 읽기 (2차 스트림)
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//파일 읽기2 (2차 스트림으로 선언)
public class FileIoEx01_02 {
public static void main(String[] args) {
BufferedInputStream bis = null;
try {
//한줄에 2차 스트림으로 선언하기
bis = new BufferedInputStream(new FileInputStream ("c:\\dirs\\text.txt") );
int data = 0;
while((data = bis.read()) != -1){
System.out.print((char)data);
}
}catch (FileNotFoundException e) {}
catch (IOException e) {}
finally{
if(bis != null) try{bis.close();} catch(IOException e){}
}
}
}
파일 쓰기 (2차 스트림)
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//파일 쓰기2 (2차 스트림으로 선언)
public class FileIoEx02_02 {
public static void main(String[] args) {
BufferedOutputStream bos = null;
try {
//한줄에 2차 스트림으로 선언하기
bos = new BufferedOutputStream(new FileOutputStream("c:\\dirs\\text2.txt", true));
bos.write('a'); bos.write('b'); bos.write('c');
bos.write('\r'); bos.write('\n'); //
bos.write('a'); bos.write('b'); bos.write('c');
} catch (FileNotFoundException e) {}
catch (IOException e) {}
finally{
if(bos != null){
try{bos.close();}catch(IOException e){}
}
}
}
}