Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JDK1.5属性使用unicode字符加载_Java_Unicode_Jdk1.5 - Fatal编程技术网

Java JDK1.5属性使用unicode字符加载

Java JDK1.5属性使用unicode字符加载,java,unicode,jdk1.5,Java,Unicode,Jdk1.5,JDK1.5属性方法仅获取InputStream,而JDK1.6+方法还获取Reader。使用load(读卡器)将带有Unicode字符的字符串加载到JDK 1.6+上的properties对象时,没有问题。但在JDK1.5上只有load(InputStream)方法;加载到属性时,未正确加载unicode字符 Properties props = new Properties(); ByteArrayInputStream bis = null; Reader reader = null; t

JDK1.5属性方法仅获取InputStream,而JDK1.6+方法还获取Reader。使用load(读卡器)将带有Unicode字符的字符串加载到JDK 1.6+上的properties对象时,没有问题。但在JDK1.5上只有load(InputStream)方法;加载到属性时,未正确加载unicode字符

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.
如何将以下带有unicode字符的示例字符串加载到properties对象

key1=test İ Ş Ğ
key2=ÇÇÇÇ
根据1.5 javadoc“假定流使用ISO 8859-1字符编码”

试试这个:

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

因此,JDK中存在工具native2ascii[.exe]

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));
            }
        }
    }
}

通常,您需要在属性文件中使用unicode转义,以便使用
InputStream
(即.\u00FF类型的替换)正确加载它们。这会导致编译错误。无法在getBytes中使用字符集。