2014-07-08 2 views
0

JDK 1.5 속성 load 메서드는 InputStream 만 가져오고 JDK 1.6+ load 메서드도 Reader를 가져옵니다. 유니 코드 문자가있는 문자열이 load (reader)가있는 JDK 1.6+의 등록 정보 객체에로드 될 때 아무런 문제가 없습니다. 그러나 JDK 1.5에는 load (InputStream) 메서드 만 있습니다. 속성에로드 될 때 유니 코드 문자가 올바르게로드되지 않습니다.JDK 1.5 속성 유니 코드 문자로로드

Properties props = new Properties(); 
ByteArrayInputStream bis = null; 
Reader reader = null; 
try { 
     bis = new ByteArrayInputStream(someStringWithUnicodeChars.getBytes("UTF-8")); 
     reader = new InputStreamReader(bis, "UTF-8"); 
    } catch (UnsupportedEncodingException ex) { 
     ex.printStackTrace(); 
    } 

props.load(reader); // This reads unicode characters correctly on JDK 1.6+ 

// There is no props.load(reader) method on JDK 1.5, so below method is used 
props.load(bis); 
// but Unicode characters are not loaded correctly. 

다음 예제 문자열을 유니 코드 문자로 특성 개체에로드 할 수 있습니까? JDK의 도구를 존재에 그에

InputStream in = new ByteArrayInputStream(someStringWithUnicodeChars.getBytes("ISO-8859-1")); 
Properties props = new Properties(); 
props.load(in); 
+0

일반적으로 당신은 특성 파일에서 유니 코드 이스케이프를 사용할 필요가 것입니다. – Kayaman

답변

1

이 시도 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html#load(java.io.InputStream)

"스트림이 가정은 ISO 8859-1 문자 인코딩을 사용한다" native2ascii [.exe]. 그들은`InputStream` (대체 즉 \ u00FF 형) 제대로로드 될 때까지

1) create the properties file as UTF-8, name it for example: sample.native 
2) convert the native properties file to Unicode escape sequences: native2ascii prop.native > prop.properties 
3) load and process the properties file 

// example: you will see the right UTF-8 characters only if your console suppert UTF-8 
class PropsFile { 
    public static void main(String[] args) throws Exception { 
     try (FileInputStream fis = new FileInputStream("sample.properties")) { 
      Properties props = new Properties(); 
      props.load(fis); 
      for (String name : props.stringPropertyNames()) { 
       System.out.println(name + "=" + props.getProperty(name)); 
      } 
     } 
    } 
} 
+0

이렇게하면 컴파일 오류가 발생합니다. charset는 getBytes에서 사용할 수 없습니다. – jdiver

0

: 1.5의 javadoc에서

key1=test İ Ş Ğ 
key2=ÇÇÇÇ 
관련 문제