简体   繁体   English

读取Java中的Unicode字符

[英]Reading Unicode characters in Java

I am using "FileInputStream" and "FileReader" to read a data from a file which contains unicode characters. 我正在使用“ FileInputStream”和“ FileReader”从包含Unicode字符的文件中读取数据。

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. 当我将默认编码设置为“ cp-1252”时,两者都在读取垃圾数据,当我将默认编码设置为UTF-8时,两者都可以读取。

  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. 当默认编码不是UTF-8时正确读取数据。

FileReader and FileWriter should IMHO be deprecated. FileReader和FileWriter应该弃用IMHO。 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") . 在这里,还有一个没有编码参数的重载版本,使用默认平台编码: System.getProperty("file.encoding")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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