我正在使用FileUtils.readFileToString一次读取一个文本文件的内容。该文件采用UTF-8编码(无BOM)。然而,我收到的不是西里尔字母?有征兆。为什么?
public String getJSON() throws IOException
{
File customersFile = new File(this.STORAGE_FILE_PATH);
return FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8);
}发布于 2020-03-04 14:01:57
FileUtils.readFileToString不能与StandardCharsets.UTF_8一起工作。
相反,尝试
FileUtils.readFileToString(customersFile, "UTF-8");
或
FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8.name());
发布于 2020-03-05 09:04:33
这是我在2015年解决这个问题的方法:
public String getJSON() throws IOException
{
// File customersFile = new File(this.STORAGE_FILE_PATH);
// return FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8);
String JSON = "";
InputStream stream = new FileInputStream(this.STORAGE_FILE_PATH);
String nextString = "";
try {
if (stream != null) {
InputStreamReader streamReader = new InputStreamReader(stream, "UTF-8");
BufferedReader reader = new BufferedReader(streamReader);
while ((nextString = reader.readLine()) != null)
JSON = new StringBuilder().append(JSON).append(nextString).toString();
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
return JSON;
}https://stackoverflow.com/questions/28478765
复制相似问题