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");
}
}
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()
출력을 위해 잠시 자료가 머무르는 출력 버퍼를 강제로 비워 자료를 출력합니다.
void close()
출력 스트림과 연결된 대상 리소스를 닫습니다. 출력 버퍼가 비워집니다.
FileOutputStream 예제
파일에 한 바이트씩 쓰기
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
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("출력이 완료되었습니다.");
}
}
byte[] 배열에 A-Z 까지 넣고 배열을 한꺼번에 파일에 쓰기
public class FileOutputStreamTest2 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("output2.txt",true);
try(fos){ //java 9 부터 제공되는 기능
byte[] bs = new byte[26];
byte data = 65; //'A' 의 아스키 값
for(int i = 0; i < bs.length; i++){ // A-Z 까지 배열에 넣기
bs[i] = data;
data++;
}
fos.write(bs); //배열 한꺼번에 출력하기
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("출력이 완료되었습니다.");
}
}
byte[] 배열의 특정 위치에서 부터 정해진 길이 만큼 쓰기
public class FileOutputStreamTest3 {
public static void main(String[] args) {
try(FileOutputStream fos = new FileOutputStream("output3.txt"))
{
byte[] bs = new byte[26];
byte data = 65; //'A' 의 아스키 값
for(int i = 0; i < bs.length; i++){ // A-Z 까지 배열에 넣기
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()메서드가 호출되면 출력 버퍼가 비워짐
댓글