728x90
반응형
안녕하세요~!
이번 학기에 자바 환경에서 게임을 개발하게 되었는데 여러 에러들을 만났고 해결하게 되었거든요!
그 방법들을 공유하고자 합니다.
이번 포스팅에서는 한글 단어들이 저장된 txt파일에서 한글을 읽어올 때 깨지는 문제를 해결하는 방법에 대해서 말씀드리겠습니다!
try {
File file = new File(파일이름);
Scanner scanner = new Scanner(new InputStreamReader(new FileInputStream(file),"UTF-8"));
/*scanner를 통해 파일 내용 읽는 코드 생략*/
scanner.close();
}
catch(FileNotFoundException e) {
System.out.println("file is not found");
System.exit(0);
}
catch(Exception e) {
System.out.println("encoding exception");
System.exit(0);
}
파일 I/O를 처리하는 java파일에서 아래 코드를 추가시키면 됩니다!
new InputStreamReader(new FileInputStream(file),"UTF-8")
이때 인코딩이 제대로 안됐을 때를 위한 Exception처리를 해야되더라구요!
위에 공유해드린 코드를 사용하시면 됩니다 :)
728x90
반응형
최근댓글