如何使用java将unicode值写入文件? [英] How to write unicode value to a file using java?

查看:109
本文介绍了如何使用java将unicode值写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 java 更新我的属性文件.我没有使用 java.util.Properties,因为它更改了键的顺序.我已经创建了自己的逻辑来按顺序编写密钥.

I am trying to update my property files using java.I am not using java.util.Properties as it changes the order of the keys . I have created my own logic to write the keys in order .

我正在使用文件 IO 来读/写属性.我面临的问题是,当我将日语字符串写入文件时,我需要对要写入的字符串的值进行 unicode.

I am using File IO to read/write properties. The problem i am facing is ,when i write the Japanese strings to the file,I need to unicode value of the String to be written.

输入:さん
文件中的预期值:\u3055\u3093
保存的价值 :さん

Input :さん
Expected value in file :\u3055\u3093
Value that is saved :さん

代码:

String strJapanese = "日本語";

try {

    FileOutputStream fos = new FileOutputStream("test.utf");

    BufferedWriter bufferedWriter = new BufferedWriter(
            new OutputStreamWriter(fos,"UTF-8"));
    bufferedWriter.write(strJapanese);
    bufferedWriter.close();

} catch (Exception e) {
    System.out.println(e.toString());
}

我也尝试将编码格式设置为 8859_1,就像 Properties 类所做的那样.在将日语字符串存储到文件时,Properties 类会自动将其转换为 unicode 字符.

I have also tried setting the encoding format to 8859_1 as done by Properties class. The Properties class automatically converts the japaneese string to unicode characters while storing it to the files.

任何帮助将不胜感激.

推荐答案

使用 org.apache.commons.lang.StringEscapeUtils#escapeJava(),这会给你Java转义字符串.>

Use org.apache.commons.lang.StringEscapeUtils#escapeJava(), that will give you Java escaped string.

String escaptedStrJapanese = StringEscapeUtils.escapeJava("日本語");
//then write it 
bufferedWriter.write(escaptedStrJapanese);

这篇关于如何使用java将unicode值写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆