프로그래밍 언어/JAVA(자바) 응용

63.바이트 단위 입출력 스트림

lroot 2022. 6. 6. 14:26
728x90
반응형

InputStream

- 바이트 단위 입력 스트림 최상위 추상 클래스

- 많은 추상 메서드가 선언되어 있고 이를 하위 스트림이 상속받아 구현함

- 주요 하위 클래스

스트림 클래스 설명
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() 입력 스트림과 연결된 대상 리소스를 닫습니다.

 

FileInputStream 예제

- 파일에서 한 바이트씩 자료 읽기

- FileInputStreamTest1 

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();
try {
fis.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}

 

- FileInputStreamTest2

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(i);
}

System.out.println("end");
}  catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}

}

}

 

- FileInputStreamTest3

public class FileInputStreamTest3 {

public static void main(String[] args) {

int i ;
try(FileInputStream fis = new FileInputStream("input2.txt")){

byte[] bs = new byte[10];

while( (i = fis.read(bs)) != -1) {

for(int j=0;j<i;j++) {
System.out.print((char)bs[j]);
}

System.out.println(" : "+i+"바이트 읽음");
}
}

 catch(IOException e) {
System.out.println(e);
}

}
}

 

OutputStream

- 바이트 단위 출력 스트림 최상위 추상 클래스

- 많은 추상 메서드가 선언되어 있고 이를 하위 스트림이 상속받아 구현함

- 주요 하위 클래스

스트림 클래스 설명
FileOutputStream 파일에서 바이트 단위로 자료를 씁니다.
ByteArrayOutputStream byte 배열 메모리에서 바이트 단위로 자료를 씁니다.
FilterOutputStream 기반 스트림에서 자료를 쓸 때 추가 기능을 제공하는 보조 스트림의 상위 클래스

- 주요 메서드

메서드 설명
int write() 한 바이트를 출력합니다.
int write(byte b[]) b[] 크기의 자료를 출력합니다.
int write(byte b[], int off, int len) b[] 배열에 있는 자료의 off위치부터 len 개수만큼 자료를 출력합니다.
void flush() 출력을 위해 잠시 자료가 머무르는 출력 버퍼를 강제로 비워 자료를 출력합니다.

 

FileOutputStream 예제

- 파일에 한 바이트씩 쓰기

- FileOutputStreamTest1.java

public class FileOutputStreamTest1 {

public static void main(String[] args) {

try(FileOutputStream fos = new FileOutputStream("output.txt")){
fos.write(65); //A
fos.write(66); //B
fos.write(67); //C
fos.write(68); //C

}catch(IOException e){
e.printStackTrace();
}
System.out.println("출력이 완료되었습니다.");

}

}

 

- FileOutputStreamTest2.java

public class FileOutputStreamTest2 {

public static void main(String[] args) throws FileNotFoundException {

FileOutputStream fos = new FileOutputStream("output2.txt",true);
try(fos){

byte[] bs = new byte[26];

byte data = 65;
for(int i = 0; i<bs.length; i++) {
bs[i] = data++;
}

fos.write(bs);

}catch(IOException e) {
System.out.println(e);
}

System.out.println("end");

}

}

 

- FileOutputStreamTest3.java

public class FileOutputStreamTest3 {

public static void main(String[] args) {

try(FileOutputStream fos = new FileOutputStream("output3.txt"))
{

byte[] bs = new byte[26];
byte data = 65;
for(int i = 0; i< bs.length; i++) {
bs[i] = data;
data++;
}
fos.write(bs,2,10); // 배열의 2번째 위치부터 10개 바이트 출력하기
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("출력이 완료되었습니다.");

}

}

 

flush() 와 close() 메서드

- 출력 버퍼를 비울때 flush() 메서드를 사용

- close() 메서드 내부에서 flush()가 호출되므로 close()메서드가 호출되면 출력 버퍼가 비워짐