0

如何将 utf-8 字符串附加到属性文件。我已经给出了下面的代码。

public  static void addNewAppIdToRootFiles() {
Properties properties = new Properties();       
   try {
       FileInputStream fin = new FileInputStream("C:\Users\sarika.sukumaran\Desktop\root\root.properties");
       properties.load(new InputStreamReader(fin, Charset.forName("UTF-8")));
       String propertyStr = new String(("قسيمات").getBytes("iso-8859-1"), "UTF-8");
       BufferedWriter bw = new BufferedWriter(new FileWriter(directoryPath + rootFiles, true));
       bw.write(propertyStr);
       bw.newLine();
       bw.flush();
       bw.close(); 
       fin.close();           

   } catch (Exception e) {
       System.out.println("Exception : " + e);
   }
} 

但是当我打开文件时,我在文件中写入的字符串“قسيمات”显示为“??????”。请帮我。

4

2 回答 2

2
于 2012-01-09T10:12:36.927 回答
2

好的,你的第一个错误是getBytes("iso-8859-1"). 您根本不应该进行这些操作。如果要将 unicode 文本写入文件,则应打开文件并写入文本。Java 中字符串的内部表示是 unicdoe,所以一切都会正确写入。

读取文件时必须关心字符集。顺便说一句,你做得对。

但是您不必使用文件操作工具将某些内容附加到properites 文件。你可以打电话prop.setProperty("yourkey", "yourvalue"),然后打电话prop.store(new FileOutputStream(youfilename))

于 2012-01-09T08:15:47.870 回答