简体   繁体   English

从文件读取特殊字符时出现问题

[英]Issue while reading special characters from file

I am facing an issue while reading a file with special characters. 读取带有特殊字符的文件时遇到问题。 My .txt file has data: 我的.txt文件包含以下数据:

I am reading this file using following code: 我正在使用以下代码读取此文件:

StringBuilder sBuilderString = new StringBuilder();

for (int n; (n = loInputStream.read()) != -1;) {
    sBuilderString.append((char)n);
}

This string is now again used to write a file, the issue is that when i write the file, one of these two characters is replaced by some other special character. 现在,此字符串再次用于写入文件,问题是当我写入文件时,这两个字符之一被其他一些特殊字符替换。

How can i write code, which is able to read all the special characters and write that to another file? 我该如何编写能够读取所有特殊字符并将其写入另一个文件的代码?

You have issues with the encoding of your characters. 您的字符编码有问题。 The call to '(char) n) will effectively transform byte n into a character using the default character encoding of your system, which might differ from the encoding of your source file. 使用系统默认的字符编码,对'(char)n)的调用将有效地将字节n转换为字符,这可能与源文件的编码不同。

One way to avoid that is to wrap your InputStream in a CharacterInputStream, where you can specify the character encoding: 避免这种情况的一种方法是将InputStream包装在CharacterInputStream中,您可以在其中指定字符编码:

Reader reader = new InputStreamReader( loInputStream, "UTF-8");

You can then proceed to read your stream into your StringBuilder. 然后,您可以继续将流读入StringBuilder。 I would also recommend to wrap your reader with a bufferedReader to improve performance with blocking IO streams. 我还建议您将您的阅读器用bufferedReader包装,以通过阻止IO流来提高性能。

Reader reader = new BufferedReader(new InputStreamReader( loInputStream, "UTF-8"));

使用InputStreamReader并指定文件中使用的编码。

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

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