Buffered In/out Stream
입출력 과정에서 메모리 버퍼를 사용해 속도를 향상시키는 클래스이다.
데이터를 버퍼에 기록하고, 한번에 모아 파일에 쓴다.
package com.java.io06_filterstream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BufferedInput01 {
public static void main(String[] args) {
// InputStream > filterInputStream >
// BufferedInputStream:버퍼 (속도차이 완화)를 활용한 입력
String path = "C:\\\\FileTest\\\\dir1\\\\test01.txt";
// 1.버퍼생성
BufferedInputStream bis = null;
// <필터(보조스트림)>----호수(메인스트림)------
try {
bis = new BufferedInputStream(new FileInputStream(path));
byte[] by = new byte[10];
while (true) {
int read = bis.read(by);
if (read == -1)
break;
String msg = new String(by, 0, read);
System.out.print(msg);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 종료 작업
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.java.io06_filterstream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedOutput01 {
public static void main(String[] args) {
// OutputStream > filterOutputStream >BufferedOutputStreamf :버퍼(속도 차이 완화) 활용한 출력
String path = "C:\\\\FileTest\\\\dir1\\\\test01.txt";
BufferedOutputStream bos = null;
try {
//1.버퍼생성
bos = new BufferedOutputStream( new FileOutputStream(path));
//2.1바이트 단위 출력
bos.write('j');
bos.write('a');
bos.write('v');
bos.write('a');
//2.2 바이트 배열
bos.write("\\r\\n".getBytes());
bos.write("즐거운 월요입니다".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//3.스트림 객체를 닫아 주어야 한다.
try {
if(bos!=null) bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}