앞서 바이트기반 스트림에 대해 배웠다.
https://standout.tistory.com/1470
스트림 - 바이트기반 스트림: FileInputStream/FileOutputStream ByteArrayInputStream/ByteArrayOutputStream PipedInputStr
앞서 스트림과 입출력에 대해 배워봤다. https://standout.tistory.com/106 스트림이란? Stream 개울, 흘러가는것, 가는길 데이터를 운반하는데 사용되는 연결통로 https://ko.wikipedia.org/wiki/%EC%8A%A4%ED%8A%B8%EB%A6%
standout.tistory.com
https://standout.tistory.com/1477
바이트기반 파일입출력 스트림 FileInputStream FileOutputStream, String File boolean FileDescriptor
FileInputStream(String name) 이 생성자는 파일 이름을 받아들여 해당 파일에 대한 입력 스트림을 연다. try { FileInputStream fis = new FileInputStream("example.txt"); // 파일에서 데이터를 읽는 작업 수행 fis.close(); }
standout.tistory.com
그중 InputStream과 OutputStream은 바이트기반 스트림의 조상이었는데,
https://standout.tistory.com/1473
바이트기반스트림 InputStream메서드 available() close() markSupported() mark() reset() read() skip()
InputStream과 OutputStream은 모든 바이트기반 스트림의 조상이다. https://standout.tistory.com/1470 스트림 - 바이트기반 스트림: FileInputStream/FileOutputStream ByteArrayInputStream/ByteArrayOutputStream PipedInputStr 앞서 스
standout.tistory.com
https://standout.tistory.com/1474
바이트기반스트림 OutputStream의 메서드 close() flush() write()
InputStream과 OutputStream은 모든 바이트기반 스트림의 조상이다. https://standout.tistory.com/1470 스트림 - 바이트기반 스트림: FileInputStream/FileOutputStream ByteArrayInputStream/ByteArrayOutputStream PipedInputStr 앞서 스
standout.tistory.com
Reader Writer또한 문자기반 스트림에서 같은역할을 한다고 이해하자.
아래는 Writer의 메서드이다.
byte배열대신 char배열을 사용한다는 것 외에는 다르지않다.
https://standout.tistory.com/1485
문자기반스트림 Reader의 메서드 close() mark() markSupported() read() ready() skip()
앞서 바이트기반 스트림에 대해 배웠다. https://standout.tistory.com/1470 스트림 - 바이트기반 스트림: FileInputStream/FileOutputStream ByteArrayInputStream/ByteArrayOutputStream PipedInputStr 앞서 스트림과 입출력에 대해
standout.tistory.com
Writer append(char c): 지정된 문자를 출력 스트림에 추가한다.
Writer append(CharSequence c): 지정된 문자 시퀀스를 출력 스트림에 추가한다.
Writer append(CharSequence c, int start, int end): 지정된 문자 시퀀스의 지정된 부분을 출력 스트림에 추가한다.
abstract void close(): 출력 스트림을 닫는다.
abstract void flush(): 출력 스트림의 버퍼를 강제로 비웁니다.
void write(int b): 지정된 바이트 값을 출력 스트림에 씁니다.
void write(char[] c): 지정된 문자 배열을 출력 스트림에 씁니다.
abstract void write(char[] c, int off, int len): 지정된 길이만큼의 문자 배열의 일부를 출력 스트림에 씁니다.
void write(String str): 지정된 문자열을 출력 스트림에 씁니다.
void write(String str, int off, int len): 지정된 길이만큼의 문자열의 일부를 출력 스트림에 씁니다.
import java.io.*;
public class WriterExample {
public static void main(String[] args) {
try {
// 파일에 데이터를 쓰기 위한 FileWriter 생성
FileWriter writer = new FileWriter("output.txt");
// append(char c) 메서드를 사용하여 문자 추가
writer.append('H');
// append(CharSequence c) 메서드를 사용하여 문자 시퀀스 추가
CharSequence charSequence = "ello";
writer.append(charSequence);
// append(CharSequence c, int start, int end) 메서드를 사용하여 문자 시퀀스 일부 추가
writer.append(charSequence, 1, 3); // 'el'
// write(int b) 메서드를 사용하여 바이트 값 쓰기
writer.write(111); // ASCII 코드 111은 문자 'o'
// write(char[] c) 메서드를 사용하여 문자 배열 쓰기
char[] chars = {' ', 'W', 'o', 'r', 'l', 'd'};
writer.write(chars);
// write(char[] c, int off, int len) 메서드를 사용하여 문자 배열의 일부 쓰기
writer.write(chars, 1, 3); // 'orl'
// write(String str) 메서드를 사용하여 문자열 쓰기
String str = "!";
writer.write(str);
// write(String str, int off, int len) 메서드를 사용하여 문자열의 일부 쓰기
writer.write(str, 0, 1); // '!'
// flush() 메서드를 사용하여 버퍼를 비우기
writer.flush();
// 출력 스트림 닫기
writer.close();
System.out.println("Data written to file successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
[출력결과]
Data written to file successfully.
'JAVA' 카테고리의 다른 글
한 스레드에서 생성한 데이터를 다른 스레드에서 읽을 수 있는 PipeReader와 PippedWriter (0) | 2024.04.11 |
---|---|
파일로부터 텍스트 데이터를 읽고, 파일에 쓰다 FileReader와 FileWriter (0) | 2024.04.11 |
문자기반스트림 Reader의 메서드 close() mark() markSupported() read() ready() skip() (0) | 2024.03.21 |
PrintStream 다양한 형태로 출력하다 PrintStream() checkError() print() println() printf() 포맷지정자 (0) | 2024.03.21 |
자동 플러시 auto flush, 버퍼가 가득차거나 출력시 전송한다 (0) | 2024.03.21 |