0

我正在使用 FileUtils.readFileToString 一次读取带有 JSON 的文本文件的内容。该文件采用 UTF-8 编码(无 BOM)。然而,我得到的不是西里尔字母??????迹象。为什么?

public String getJSON() throws IOException
{
    File customersFile = new File(this.STORAGE_FILE_PATH);
    return FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8);
}
4

2 回答 2

0

FileUtils.readFileToString不适用于StandardCharsets.UTF_8.

相反,尝试

FileUtils.readFileToString(customersFile, "UTF-8");

或者

FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8.name());

于 2020-03-04T06:01:57.330 回答
0

这就是我在 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;
}
于 2020-03-05T01:04:33.350 回答