简体   繁体   中英

Reading Unicode characters in Java

I am using "FileInputStream" and "FileReader" to read a data from a file which contains unicode characters.

When i am setting the default encoding to "cp-1252" both are reading junk data, when i am setting default encoding to UTF-8 both are reading fine.

  1. Is it true that both these use System Default Encoding to read the data?
  2. Then whats the benifit of using Character stream if it depends on System Encoding.
  3. Is there any way apart from:

      BufferedReader fis = new BufferedReader(new InputStreamReader(new FileInputStream("some unicode file"),"UTF-8")); 

    to read the data correctly when the default encoding is other than UTF-8.

FileReader and FileWriter should IMHO be deprecated. Use

new InputStreamReader(new FileInputStream(file), "UTF-8")

or so.

Here also there exists an overloaded version without the encoding parameter, using the default platform encoding: System.getProperty("file.encoding") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM