Read special symbols from file

后端 未结 2 1389
刺人心
刺人心 2020-12-12 07:47

I am trying to read file using Java and the file also containing special characters also. I am trying to write the contents of file into another file.

What is the so

相关标签:
2条回答
  • 2020-12-12 08:26

    You need to find out the exact encoding that the file uses, and then specify that in the InputStreamReader parameter.

    Nevertheless it is often best to reader character data by wrapping your InputStream with an InputStreamReader into Reader or BufferedReader

    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(your file), "UTF-8"));
    

    Edit For Comment:
    FileReader uses Java's platform default encoding, which depends on the system settings of the computer on which its running on.

    Unfortunately FileReader does not allow to set encoding. Instead, you have to use Above code to get the desire encoding for your file.

    0 讨论(0)
  • 2020-12-12 08:38

    Encode file in UTF-8 and use java encoding aware streams operators:

    new InputStreamReader(new FileInputStream(file), "UTF-8")
    
    0 讨论(0)
提交回复
热议问题