java.io패키지
그림, 멀티미디어, 문자 등 모든 종류의 데이터를 받고 보내는 것 가능,주로 문자 사용 (바이트 1byte씩 문자 2byte씩 보냄)
최상위 클래스
InputStream is = new FileInputStream("C:/test.jpg");
int readByte;
byte[] readBytes = new byte[100] //read(byte[] b) 많은 데이터를 읽을 때 효과적
int readByteNo = is.read(readBytes); //차이점
int readByteNo. = is.read(readBytes,0,100); //차이점
while((readByte = is.read(readBytes)) != -1) {}
❗️많은 양의 바이트를 읽을 때는 read(byte[] b) 메소드를 사용하는 것이 좋음
최상위 클래스
OutputStream os = new FileOutputStream("C:/test.txt");
byte[] data = "ABC".getByte();
for(int i=0; i<data.length; i++){
os.write(data[i]); // "A","B","C"를 하나씩 출력
}
os.write(data); // "ABC" 모두 출력
os.wirte(data,1,2); //"BC"만 출력
os.flush(); //남은 버퍼의 데이터를 출력
os.close();
최상위 클래스
Reader reader = new FileInputStream("C:/text.txt");
int readDate;
while((readDate = reader.read()) != -1) {
char charData = (char) readData; //읽은 문자를 얻을 수 있음
}
Reader reader = new FileInputStream("C:/text.txt");
int readCharNo;
int readCharNo = reader.read(cbuf); //차이점
int readCharNo = reader.read(cbuf,0,100); //차이점
char[] cbuf = new char[2];
while((readCharNo = reader.read(cbuf)) != -1) {}
최상위 클래스
Writer writer = new FileWriter("C:/test.txt");
char[] data = "홍길동".toCharArray();
for(int i=0; i<data.length; i++){
writer.write(data[i]); // "홍","길","동"을 하나씩 출력
}
writer.write(data); // "홍길동" 모두 출력
writer.write(data,1,2); //"길동"만 출력
writer.flush();
writer.close();
문자 출력 스트림은 내부에 작은 버퍼(buffer)가 있어서 문자가 출력되기 전에 버퍼에 쌓여있다가 순서대로 출력됨
flush()메소드는 버퍼에 잔류하고 있는 문자를 모두 출력시키고 버퍼를 비움
프로그램에서 더 이상 출력할 문자가 없다면 flush()메소드를 마지막으로 호출하여 버퍼에 잔류하는 모든 문자가 출력되도록 해야함
Writer를 더 이상 사용하지 않을 경우에는 close()메소드를 호출해서 Writer에서 사용했던 시스템 자원을 풀어줌