我正在尝试读取包含 unicode 字符的文件,将这些字符转换为相应的符号,然后将生成的文本打印到新文件中。我正在尝试使用 StringEscapeUtils.unescapeHtml 来执行此操作,但这些行只是按原样打印,unicode 点仍然完好无损。我做了一个练习,从文件中复制一行,从中创建一个字符串,然后在上面调用 StringEscapeUtils.unescapeHtml,效果很好。我的代码如下:
class FileWrite
{
public static void main(String args[])
{
try{
String testString = " \"text\":\"Dude With Knit Hat At Party Calls Beer \u2018Libations\u2019 http://t.co/rop8NSnRFu\" ";
FileReader instream = new FileReader("Home Timeline.txt");
BufferedReader b = new BufferedReader(instream);
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(StringEscapeUtils.unescapeHtml3(testString) + "\n");//This gives the desired output,
//with unicode points converted
String line = b.readLine().toString();
while(line != null){
out.write(StringEscapeUtils.unescapeHtml3(line) + "\n");
line = b.readLine();
}
//Close the output streams
b.close();
out.close();
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}