Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- transaction
- pointcut
- db
- 인프런
- jpa
- 자바
- QueryDSL
- 김영한
- 그리디
- Spring Boot
- JDBC
- 스프링 핵심 원리
- 스프링
- Thymeleaf
- Android
- http
- 알고리즘
- Greedy
- java
- 백준
- spring
- springdatajpa
- JPQL
- Servlet
- Proxy
- Exception
- SpringBoot
- 스프링 핵심 기능
- kotlin
- AOP
Archives
- Today
- Total
개발자되기 프로젝트
바이트 단위 출력 스트림 본문
1.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() | 출력 스트림과 연결된 대상 리소스를 닫습니다. 출력 버퍼가 비워집니다. |
2. FileOutputStream 예제 : 한 바이트 씩 쓰기
- 찾으려는 파일이 없는 경우 새로 만듦
- 파일에 쓰는 방법은 기본적으로 overwrite
- 이어쓰고 싶은 경우? true 설정.
FileOutputStream fos = new FileOutputStream("output.txt", true);
public class FileOutputStreamTest {
public static void main(String[] args) {
try(FileOutputStream fos = new FileOutputStream("output.txt")){
fos.write(65);
fos.write(66);
fos.write(67);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end");
}
}
- output.txt가 생성되고 ABC가 입력됨.
3. FileOutputStream 예제 : 배열은 한꺼번에 파일에 쓰기
- byte[]배열에 A-Z넣고 한번에 써보자.
public class FileOutputStreamTest2 {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fos = new FileOutputStream("output.txt");
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) {
e.printStackTrace();
}
System.out.println("end");
}
}
4. flush() 와 close() 메서드
- 출력 버퍼를 비울때 flush() 메서드를 사용
- close() 메서드 내부에서 flush()가 호출되므로 close()메서드가 호출되면 출력 버퍼가 비워짐
5. GitHub : 211031 Output Stream
GitHub - bsh6463/various_functions
Contribute to bsh6463/various_functions development by creating an account on GitHub.
github.com
'Java > 다양한 기능' 카테고리의 다른 글
보조 스트림 (0) | 2021.10.31 |
---|---|
문자단위 입출력 스트림 (0) | 2021.10.31 |
바이트 단위 입출력 스트림 (0) | 2021.10.31 |
표준 입출력 스트림 (0) | 2021.10.31 |
자바 입출력을 위한 I/O Stream (0) | 2021.10.31 |
Comments