<바이트 기반 스트림>
1. FileOutputStream 클래스
1. 바이트 기반의 출력 스트림
2. 모든 것을 보낼 때 사용
1) 통신 : 상대방에게 데이터를 보낼 때
2) 파일 : 모든 파일을 만들 때
3. 출력 메소드
write() /*String은 byte로 변환해 write()하기
4. 출력 단위
int(한개보낼), byte[](여러개 보낼때)
2. FileInputStream 클래스
1. 바이트 기반의 입력 스트림이다.
2. 모든 데이터를 읽을 때 사용한다.
1) 통신 : 상대방이 보낸 데이터를 읽을 때
2) 파일 : 모든 파일을 읽을 때
3. 입력메소드
read()
4. 입력 단위
int(한 개 읽을 때), byte[] (여러개 읽을 때)
(*한글 깨짐)
- 예제(self)
import java.io.FileOutputStream;
import java.io.IOException;
public class fileOutput {
public static void main(String[] args) {
try(FileOutputStream fos = new FileOutputStream("text1.txt")){
String str1 = "지금은 입출력 자바 연습중";
String str2 = "화이팅합시다!";
fos.write( str1.getBytes());
fos.write('\n');
fos.write(str2.getBytes());
System.out.println("byte FileOutputStream 성공");
}catch(IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class fileInput {
public static void main(String[] args) {
try (FileInputStream is = new FileInputStream("text1.txt")){
int ch = 0;
while( (ch = is.read()) != -1) {
System.out.print((char)ch);
}
System.out.println("byte FileInputStream 성공 ");
} catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
<바이트 기반 보조 스트림>
BufferedIn/OutputStream 클래스
1. 바이트 기반의 출력 보조 스트림이다.
2. FileIn/Output 클래스와 같은 메인스트림과 함께 사용한다.
3. 내부 버퍼를 이용해서 동작 속도를 향상시킨다.
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ex1_Bufferedoutputstream {
public static void main(String[] args) {
try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("text2.txt"))){
String str1 = "안녕하세요 지금은 바이트 보조스트림 복습 중입니다. ";
bos.write(str1.getBytes());
System.out.println("byte BufferedoutputStream success");
}catch(IOException e) {
}
}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ex2_BufferedInputStream {
public static void main(String[] args) {
try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream("text2.txt"))){
byte[] b = new byte[10];
int readCount = 0;
if( (readCount = bis.read(b)) != -1) {
System.out.println(new String(b, 0, readCount));
}
System.out.println("BufferedInputStream success");
} catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
'국비 > JAVA' 카테고리의 다른 글
문자 기반 (보조)Stream (0) | 2021.09.02 |
---|---|
컬렉션 프레임워크 (0) | 2021.09.01 |
LOMBOK (0) | 2021.09.01 |
상속_추상클래스와 인터페이스 (0) | 2021.08.30 |
메서드,반복문, 조건문을 이용한 자판기만들기 (0) | 2021.08.29 |
댓글