Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java text 읽을 때 한글 깨짐 해결 #1

Open
WonYong-Jang opened this issue May 4, 2018 · 1 comment
Open

Java text 읽을 때 한글 깨짐 해결 #1

WonYong-Jang opened this issue May 4, 2018 · 1 comment

Comments

@WonYong-Jang
Copy link
Owner

아래와 같이 해결

자바로 txt 파일을 읽을 때 

1. 일반적인 방법

File file    =  new File("/home/smilem/SM-201104021025.txt");
   
   BufferedReader br  =  new BufferedReader(new FileReader(file));

String line = null;

while((line=br.readLine())!=null){
System.out.println(line);
}

2. 내가 사용한 방법

   File file    =  new File("/home/smilem/SM-201104021025.txt");
   
   BufferedReader br  =  new BufferedReader(new InputStreamReader(new FileInputStream(file),"euc-kr"));

String line    =  null;

if((line=br.readLine()) != null){
System.out.println(line);
}

1번에서 FileReader 클래스로 읽게 되면 임의의 캐릭터셋으로 byte 인코딩 해서 읽게 된다.

그러면 이미 line 변수로 들어오게 되면 데이터가 깨져있므로 이 line 을 아무리 인코딩하고

난리 부르스를 쳐도 다시 되돌릴 수가 없다

그런데 2번에서는 아예 읽을 때 수동으로 byte 데이터를 인코딩 해서 읽도록 하였다

그래서 위의 방법은 잘 돌아간다.


출처: http://shonm.tistory.com/category/JAVA/TEXT파일 읽을 때 한글 깨짐 [정윤재의 정리노트]
@WonYong-Jang
Copy link
Owner Author

작성된 텍스트 파일 형식에 따라서 euc-kr, utf-8 등으로 지정

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant