자바에서 한글 출력이 이상합니다.

vudghkzm의 이미지

리눅스를 사용중이고, 자바는 1.4.2 사용중입니다.
시스템 환경변수 LANG은 ko_KR.EUC-KR 로 설정된 상태입니다.

다음의 2 종류의 코드가 서로 다른 출력값을 가지는데 왜 그런지 도무지 모르겠습니다. (편의상 import 는 생략했습니다.)

public class Main {
    public static void main(String args[]) throws Exception {
        String file = "a.properties";
        FileInputStream propFileStream = new FileInputStream(file);
        Properties properties = new Properties();
        properties.load(propFileStream);

        String e = properties.getProeprty("e");
        System.out.println(e);        
    }
}

public class Main {
    public static void main(String args[]) throws Exception {
        String e = "한글";
        System.out.println(e);        
    }
}

아래는 첫번째 코드에서 사용한 a.properties 파일입니다.
e = 한글

첫번째 코드의 결과는 ??±? 이와 같이 깨져서 출력이 됩니다.
두번째 코드의 결과는 깨끗하게 "한글" 로 출력이 됩니다.
그런데 첫번째 코드를 다음과 같이 수정하면 깨끗하게 "한글"로 출력이 되네요.

public class Main {
    public static void main(String args[]) throws Exception {
        String file = "a.properties";
        FileInputStream propFileStream = new FileInputStream(file);
        Properties properties = new Properties();
        properties.load(propFileStream);

        String e = properties.getProeprty("e");
        e = new String(e.getBytes("ISO-8859-1"), "euc-kr"); // 추가한 부분
        System.out.println(e);        
    }
}

property 파일에서 값을 얻어와서 콘솔에 출력해야 한는 구문이 굉장히 많은데, 일일이 저렇게 코드를 다시 삽입한다는게 굉장히 귀찮네요.

도대체 뭐가 문제일까요?

저렇게 추가 코드를 삽입하지 않고, 본래 첫번째 코드로만 해도 깨끗하게 "한글"로 출력되게 할 수 있는 방법은 없나요?
자바는 내부적으로 전부 유니코드로 처리한다고 해서 그냥 아무 생각없이 프로그래밍하면 되는줄 알았는데... ㅠ.ㅠ

답변 부탁드립니다.

jongwooh의 이미지

이 문제는 에디터와 관련이 있습니다.

에디터를 유니코드를 지원하는 에디터로 소스를 작성하면 (즉 소스 자체가 유니코드로 작성되면) 해결되는 경우가 있습니다.

자바 컴파일러 자체가 ASCII+EUC_KR 소스코드와 유니코드 소스코드를 모두 이해하지만 컴파일시에 코드셋 자체는 다르게 적용시키는데, 사람이 소스 프로그램의 코드셋은 잘 생각하지 않고 만들기 때문에 생기는 문제라고 말할 수 있습니다.

you must know the power of dark side.

M.W.Park의 이미지

native2ascii를 사용하셔야합니다.

-----
오늘 의 취미는 끝없는, 끝없는 인내다. 1973 法頂

purple의 이미지

에디터와 컴파일 환경도 영향을 받지만 둘 다 제대로 되어도 이 상황에서는 위와 같은 결과가 나오는 것이 제대로 된 동작입니다.

자바의 properties 파일은 iso-8859-1 인코딩으로 동작합니다(언어 환경과 상관없이). 그외의 문자를 입력하려면 unicode escape를 하셔야 하는데 그 작업을 해주는 툴이 native2ascii 입니다.

참조 http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html

vudghkzm의 이미지

 
$ native2ascii -encoding EUC-KR a.properties a.properties

위와 같이 실행하니 올바로 출력이 되네요.

모두 감사드립니다. :D

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.