how to parse unicode that is read from a file in java [duplicate]

假装没事ソ 提交于 2019-11-30 16:12:06
Grega Kešpret

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!