입력 스트림: FileInputStream, FileReader, BufferedInputStream, BufferedReader등
출력 스트림: FileOutputStream,FileWirter,BufferedOutputStream,BufferedWriter등
바이트 스트림: FileInputStream, FileOutputStream,BufferedInputStream,BufferedOutputStream
문자 스트림: FileReader,FileWriter,BufferedReader,BufferedWriter등
기반 스트림: FileInputStream, FileOutputStream, FileReader,FileWriter등
보조 스트림: InputStreamReader, OutputStreamWriter, BufferedInputStream, BufferedOutputStream등
public class System{
public static PrintStream out;
public static InputStream in;
public static PrintStream err;
}
바이트 단위 입력 스트림 최상위 추상 클래스
많은 추상 메서드가 선언되어 있고 이를 하위 스트림이 상속받아 구현함
주요 하위 클래스
FileInputStream: 파일에서 바이트 단위로 자료를 읽습니다.
ByteArrayInputStream: byte 배열 메모리에서 바이트 단위로 자료를 읽습니다.
FilterInputStream: 기반 스트림에서 자료를 읽을 때 추가 기능을 제공하는 보조 스트림의 상위 클래스
주요 메서드
int read() : 입력 스트림으로부터 한 바이트의 자료를 읽습니다. 읽은 자료의 바이트 수를 반환합니다.
int read(byte b[]) :입력 스트림으로 부터 b[]크기의 자료를 b[]에 읽는다. 읽은 자료의 바이트 수를 반환한다
int read(byte b[], int off,int len) : 읽은 스트림으로 부터 b[] 크기의 자료를 b[]의 off변수 위치로 부터 저장하며 len만큼 읽는다 읽은 자료의 바이트 수를 반환
void close():입력 스트림과 연결된 대상 리소스를 닫는다.
public class FileInputStreamTest1 {
public static void main(String[] args) {
FileInputStream fis=null;
try {
fis=new FileInputStream("input.txt");
System.out.println((char)fis.read());
System.out.println((char)fis.read());
System.out.println((char)fis.read());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e2){
System.out.println(e2);
}
}
System.out.println("end");
}
}
public class FileInputStreamTest2 {
public static void main(String[] args) {
try(FileInputStream fis = new FileInputStream("input.txt")){
int i;
while ( (i = fis.read()) != -1){
System.out.println((char)i);
}
System.out.println("end");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class FileInputStreamTest3 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("input2.txt")){
byte[] bs = new byte[10];
int i;
while ( (i = fis.read(bs)) != -1){
/*for(byte b : bs){
System.out.print((char)b);
}*/
for(int k= 0; k<i; k++){
System.out.print((char)bs[k]);
}
System.out.println(": " +i + "바이트 읽음" );
}
/*while ( (i = fis.read(bs, 1, 9)) != -1){
for(int k= 0; k<i; k++){
System.out.print((char)bs[k]);
}
System.out.println(": " +i + "바이트 읽음" );
}*/
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end");
}
}
QRST가 남아있어서 3회차때 QRST도 같이 보인다.
바이트 단위 출력 스트림 최상위 추상클래스
많은 추상 메서드가 선언되어 있고 이를 하위 스트림이 상속받아 구현함
주요 하위 클래스
FileOutputStream: 파일에서 바이트 단위로 자료를 씀
ByteArrayOutputStream: byte배열 메모리에서 바이트 단위로 자료를 씀
FilterOutputStream :기반 스트림에서 자료를 쓸 때 추가 기능을 제공하는 보조 스트림의 상위 클래스
주요 메서드
int write(): 한바이트를 출력
int wirte(byte b[]) : b[]크기의 자료를 출력
int write(byte b[],int off,int len): b[]배열에 있는 자료의 off위치부터 len개수 만큼 자료를 출력
void flush(): 출력을 위해 잠시 자료가 머무르는 출력 버퍼를 강제로 비워 자료를 출력
void close(): 출력 스트림과 연결된 대상 리소스를 닫는다. 출력 버퍼가 비워짐
문자 단위 입력 스트림 최상위 추상 클래스
많은 추상 메서드가 선언되어 있고 이를 하위 스트림이 상속받아 구현함
주요 하위 클래스
FileReader :파일에서 문자 단위로 읽는 스트림 클래스
InputStreamReader : 바이트 단위로 읽은 자료를 문자로 변환해주는 보조 스트림 클래스
BufferedReader: 문자로 읽을 때 배열을 제공하여 한꺼번에 읽을 수 있는 기능을 제공하는 보조 스트림
주요 메서드
int read() : 파일로부터 한 문자를 읽는다. 읽은 문자를 반환
int read(char[] buf) : 파일로부터 buf 배열에 문자를 읽는다
int read(char[] buf, int off, int len) :파일로부터 buf 배열의 off위치로부터 len 개수만큼의 문자를 읽는다
void close(): 입력 스트림과 연결된 대상 리소스를 닫는다
public class FileReaderTest {
public static void main(String[] args) {
try(FileReader fr=new FileReader("reader.txt")){
int i;
while ((i =fr.read())!=-1){
System.out.print((char) i);
}
}catch(IOException e){
System.out.println(e);
}
}
}
문자 단위 출력 스트림 최상위 추상 클래스
많은 추상 메서드가 선언되어 있고 이를 하위 스트림이 상속받아 구현
주요 하위 클래스
FileWriter : 파일에서 문자 단위로 출력하는 스트림 클래스
OutputStreamWriter : 바이트 단위의 자료를 문자로 변환해 출력해주는 보조 스트림 클래스
BufferedWriter: 문자로 쓸 때 배열을 제공하여 한꺼번에 쓸 수 있는 기능을 제공해하는 보조 스트림
주요 메서드
int write(int c) :한 문자를 파일에 합니다
int write(char[] buf): 문자 배열 buf의 내용을 출력
int write(char[] buf,int off,int len):문자 배열 buf의 off위치부터 len 개수의 문자를 출력
int write(String str): 문자열 str을 출력
int wirte(String str, int off, int len): 문자열 str의 off번째 문자로부터 len개수 만큼 출력
int flush() : 출력하기 전에 자료가 있는 공간 (출력 버퍼)을 비워 출력하도록 함
void close(): 스트림과 연결된 리소스를 닫음, 출력 버퍼도 비워짐
protected FilterInputStream과 (InputStream in) : 생성자의 매개변수로 InputStream을 받는다
public FilterOutputStream(OutputStream out): 생성자의 매개변수로 OutputStream을 받는다
public class InputStreamReaderTest {
public static void main(String[] args) {
try(InputStreamReader isr = new InputStreamReader(new FileInputStream("reader.txt"))){
int i;
while( (i = isr.read()) != -1){ //보조 스트림으로 읽습니다.
System.out.print((char)i);
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
public class BufferedStreamTest {
public static void main(String[] args) {
long millisecond = 0;
try(FileInputStream fis = new FileInputStream("a.zip");
FileOutputStream fos = new FileOutputStream("copy.zip");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos)){
millisecond = System.currentTimeMillis();
int i;
while( ( i = bis.read()) != -1){
bos.write(i);
}
millisecond = System.currentTimeMillis() - millisecond;
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("파일 복사 하는 데 " + millisecond + " milliseconds 소요되었습니다.");
}
}