-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterConvertStreamExample.java
More file actions
35 lines (30 loc) · 1.24 KB
/
CharacterConvertStreamExample.java
File metadata and controls
35 lines (30 loc) · 1.24 KB
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
29
30
31
32
33
34
35
package ch18.sec06;
import java.io.*;
public class CharacterConvertStreamExample {
public static void main(String[] args) throws IOException {
writer("문자 변환 스트림을 사용합니다.");
String data = read();
System.out.println(data);
}
private static void writer(String str) throws IOException {
// FileOutputStream 에 OutputStreamWriter 보조 스트림 연결
OutputStream os = new FileOutputStream("temp/test.txt");
Writer writer = new OutputStreamWriter(os);
// OutputStreamWriter 보조 스트림을 이용해 문자 출력
writer.write(str);
writer.flush();
writer.close();
}
private static String read() throws IOException {
// FileInputStream 에 InputStreamReader 보조 스트림을 연결
InputStream is = new FileInputStream("temp/test.txt");
Reader reader = new InputStreamReader(is, "UTF-8");
// InputStreamReader 보조 스트림을 이용해서 문자 입력
char[] data = new char[100];
int num = reader.read(data);
reader.close();
// char 배열에서 읽은 문자 수만큼 문자열로 변환
String str = new String(data, 0, num);
return str;
}
}