IO - InputStream, OutputStream, Reader, Writer

🥔김감자🥔·2022년 12월 5일

Stream

  • 자바에서 데이터는 스트림(Stream)을 통해 입출력된다.
  • 프로그램이 데이터를 입력받을 때에는 입력 스트림(InputStream)이라 부르고, 프로그램이 데이터를 보낼 때에는 출력 스트림 (OutputStream)이라 부른다.

1. InputStream

바이트(Byte) 기반 입력 스트림의 최상위 클래스로 추상 클래스이다.

int read(byte[] buf) throws IOException
  • 입력 스트림으로부터 매개값으로 주어진 바이트 배열의 길이만큼 바이트를 읽고 배열에 저장한다.
  • 읽어들인 바이트 수를 리턴한다.
  • 더 이상 읽을 수 없다면 -1을 리턴한다.
InputStream is = new FileInputStream("C:/test.jpg");

int readByteNo;
byte[] readBytes = new byte[100];

while((readByteNo = is.read(readByte)) != -1) {
	...
}

int read(byte[] buf, int off, int len) throws IOException
  • 입력 스트림으로부터 len개의 바이트만큼 읽고, 매개값으로 주어진 바이트 배열 buff[off]부터 len개까지 저장한다.
  • 읽어들인 바이트 수를 리턴한다.
  • 더 이상 읽을 수 없다면 -1을 리턴한다.
 InputStream is = ...;
 
 byte[] readBytes = new byte[100];
 int readByteNo = is.read(readBytes, 0, 100);



2. OutputStream

바이트(Byte) 기반 출력 스트림의 최상위 클래스로 추상 클래스이다.

void write(byte[] buf) throws IOException
  • 매개 값으로 주어진 바이트 배열의 모든 바이트를 출력 스트림으로 보낸다.
OutputStream os = new FileOutputStream("C:/test.text");
byte[] data = "ABC".getBytes();
os.write(data)		// "ABC" 모두 출력

void write(byte[] buf, int off, int len) throws IOException
  • buff[off]개부터 len개의 바이트를 출력 스트림으로 보낸다.
OutputStream os = new FileOutputStream("C:/test.txt");
byte[] data = "ABC".getBytes();
os.write(data, 1, 2);	// "BC" 출



3. Reader

문자 기반 입력 스트림의 최상위 클래스로 추상 클래스이다.

int read() throws IOException
  • 입력 스트림으로부터 한 개의 문자(2바이트)를 읽고 4바이트 int 타입으로 리턴한다.
  • 리턴된 4바이트 중 끝에 있는 2바이트에 문자 데이터가 들어있다.
  • 더 이상 읽을 수 없다면 -1을 리턴한다.
char charData = (char) read();

Reader reader = new FileReader("C:/test.txt");
int readData;

while ((readData = reader.read()) != -1){
	char charData = (char) readData;
}

int read(byte[] cbuf) throws IOException
  • 입력 스트림으로부터 매개값으로 주어진 문자 배열의 길이만큼 문자를 읽고 배열에 저장한다.
  • 읽어들인 문자 수를 리턴한다.
  • 더 이상 읽을 수 없다면 -1을 리턴한다.
Reader reader = new Reader("C:/test.txt");
int readCharNo;
char[] cbuf = new char[2];

while ((readCharNo = reader.read(cbuf)) != -1){
	...
}

abstract int read(byte[] cbuf, int off, int len) throws IOException
  • 입력 스트림으로부터 len개의 문자만큼 읽고, 매개값으로 주어진 문자 배열 cbuf[off]부터 len개까지 저장한다.
  • 읽어들인 문자 수를 리턴한다.
  • 더 이상 읽을 수 없다면 -1을 리턴한다.
Reader reader = ...;
byte[] cbuf = new cbuf[100];
int readCharNo = reader.read(cbuf, 0, 100);



4. Writer

문자 기반 출력 스트림의 최상위 클래스로 추상 클래스이다.

void write(int c) throws IOException
  • 매개 변수로 주어진 int 값에서 끝에 있는 2바이트(한 개의 문자)만 출력 스트림으로 보낸다.
  • * 매개 변수가 int 타입이므로 4바이트 모두를 보내는 것으로 오해할 수 있으니 주의! *
Writer writer = new FileWriter("C:/test.txt");
char[] data = "홍길동".toCharArray();
for(int i = 0; i < data.length; ++i){
	writer.write(data[i]);		// "홍", "길", "동"을 하나씩 출력
}

void write(char[] cbuf) throws IOException
  • 매개 값으로 주어진 char[] 배열의 모든 문자를 출력 스트림으로 보낸다.
Writer writer = new FileWriter("C:/test.txt");
char[] data = "홍길동".toCharArray();
writer.write(data); 	// "홍길동" 모두 출력

abstract void write(char[] cbuf, int off, int len) throws IOException
  • cbuff[off]부터 len개의 문자를 출력 스트림으로 보낸다.
Writer writer = new FileWriter("C:/test.txt");
byte[] cbuf = new cbuf[100];
writer.write(data, ); 	// "홍길동" 모두 출력
profile
감자를 캐자 감자를 캐자

0개의 댓글