how to parse unicode that is read from a file in j

2019-02-11 05:26发布

This question already has an answer here:

I have written a text file with the following contents: \u0032\u0142o\u017Cy\u0142

Then I have used FileReader und BufferedReader to read the file.

public static void main(String[] args) throws Exception{
   FileInputStream fr = new FileInputStream("README.TXT");
   BufferedReader br = new BufferedReader(new InputStreamReader(fr,"UTF-8"));
   String s="";
   while((s=br.readLine())!=null){
      System.out.println(s);
    }
}

But the output is: \u0032\u0142o\u017Cy\u0142.

When I used

System.out.println("\u0032\u0142o\u017Cy\u0142");

These codes will be parsed and will be shown in the right form.

How can I change my code, so that unicode from the files will also be parsed and shown in the right form?

3条回答
姐就是有狂的资本
2楼-- · 2019-02-11 05:59

The parsing of unicode escape sequences is not an explicit part of the Java Standard API, it only implicitly occurs when loading Properties. You could copy the implementation from the source code of Properties.

But it would be better to use a normal encoding like UTF-8 for your file.

查看更多
Anthone
3楼-- · 2019-02-11 06:07

You can use the source code posted here to do unescaping.

查看更多
Explosion°爆炸
4楼-- · 2019-02-11 06:09

You want to use sun.tools.native2ascii to reverse convert the text.

new sun.tools.native2ascii.Main().convert(new String[]{"-reverse", new File("README.TXT"), convertedFile});

So something like this will do it.

public static void main(String[] args) throws Exception{
   File convertedFile = new File("converted.txt");
   new sun.tools.native2ascii.Main().convert(new String[]{"-reverse", new File("README.TXT"), convertedFile});
   FileInputStream fr = new FileInputStream(convertedFile);
   BufferedReader br = new BufferedReader(new InputStreamReader(fr,"UTF-8"));
   String s="";
   while((s=br.readLine())!=null){
      System.out.println(s);
    }
}
查看更多
登录 后发表回答