quilt code
[고급자바] IO (2) 본문
1. FileReader
|
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
|
package kr.or.ddit.basic;
import java.io.FileReader;
import java.io.IOException;
public class T08FileReaderTest {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("d:/D_Other/testChar.txt");
int data = 0;
while((data = fr.read()) != -1) {
System.out.println((char) data);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|
cs |
2. FileEncoding
인코딩 방식)
한글 인코딩 방식은 크게 UTF-8과 EUC-KR 방식 두가지로 나누어 볼 수 있다. 원래 한글 윈도우는 CP949 방식을 사용했는데 윈도우를 개발한 마이크로소프트 사에서 EUC-KR 방식에서 확장하였기 때문에 MS949라고도 부른다. 한글 윈도우의 메모장에서 말하는 ANSI 인코딩이란, CP949(Code Page 949)를 말한다. CP949는 EUC-KR의 확장이며, 하위 호환성이 있다. - MS949 : 윈도우의 기본 한글 인코딩 방식(ANSI 계열) - UTF-8 : 유니코드 UTF-8 인코딩 방식(영문자 및 숫자: 1byte, 한글: 3byte) => 가변적 - US-ASCII : 영문전용 인코딩 방식 ANSI는 영어를 표기하기 위해 만든 코드 규격으로 자체에 한글이 없었다가 나중에 EUC-KR, CP949라는 식으로 확장되면서 한글이 포함되었음. |
|
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
36
37
38
39
40
41
42
43
|
package kr.or.ddit.basic;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class T09FileEncodingTest {
public static void main(String[] args) {
FileInputStream fis = null;
InputStreamReader isr = null;
try {
//fis = new FileInputStream("d:/D_Other/test_ansi.txt");
fis = new FileInputStream("d:/D_Other/test_utf8.txt");
// 파일 인코딩 정보를 이용하여 읽어오기
// InputStreamReader 객체는 파일의 인코딩 방식을 지정할 수 있다.
//isr = new InputStreamReader(fis,"ms949");
isr = new InputStreamReader(fis);
int data = 0;
while((data = isr.read()) != -1 ) {
System.out.print((char) data);
}
System.out.println();
System.out.println("출력 끝...");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
isr.close(); // 보조스트림만 닫아도 된다.
} catch (IOException e) {
}
}
}
}
|
cs |
|
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
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package kr.or.ddit.basic;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class T10FileEncodingTest {
/*
* 키보드로 입력한 내용을 파일로 저장하는데
* out_utf8.txt파일은 'UTF-8' 인코딩 방식으로
* out_ansi.txt파일은 'ms949' 인코딩 방식으로 저장한다.
*/
public static void main(String[] args) throws IOException {
/*
* OutputStreamWriter => OutputStream(바이트기반 스트림)을 Writer(문자기반스트림)로 변환해 주는 보조 스트림
* 이 객체도 출력할 때 '인코딩 방식'을 지정해서 출력 할 수 있다.
*/
// 파일 출력용
FileOutputStream fos1 = new FileOutputStream("d:/D_Other/out_utf8.txt");
FileOutputStream fos2 = new FileOutputStream("d:/D_Other/out_ansi.txt");
OutputStreamWriter osw1 = new OutputStreamWriter(fos1, "utf-8");
OutputStreamWriter osw2 = new OutputStreamWriter(fos2, "cp949");
System.out.println("아무거나 입력하세요.");
InputStreamReader isr = new InputStreamReader(System.in);
int data = 0;
while((data = isr.read()) != -1 ) {
osw1.write(data);
osw2.write(data);
}
System.out.println("작업 완료..."); // enter 치고 ctrl + z 눌러서 입력 완료
isr.close();
osw1.close();
osw2.close();
}
}
|
cs |
'daily > 고급자바' 카테고리의 다른 글
| [고급자바] IO (4) (0) | 2023.02.14 |
|---|---|
| [고급자바] IO (3) (0) | 2023.02.13 |
| [고급자바] IO (1) (0) | 2023.02.13 |
| [고급자바] Thread(6) (0) | 2023.02.13 |
| [고급자바] Thread(5) (0) | 2023.02.10 |