首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >来自Apache Commons IO的FileUtils.readFileToString()无法正确处理西里尔文

来自Apache Commons IO的FileUtils.readFileToString()无法正确处理西里尔文
EN

Stack Overflow用户
提问于 2015-02-12 21:32:24
回答 2查看 3K关注 0票数 0

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

代码语言:javascript
运行
复制
public String getJSON() throws IOException
{
    File customersFile = new File(this.STORAGE_FILE_PATH);
    return FileUtils.readFileToString(customersFile, StandardCharsets.UTF_8);
}
EN

回答 2

Stack Overflow用户

发布于 2020-03-04 14:01:57

FileUtils.readFileToString不能与StandardCharsets.UTF_8一起工作。

相反,尝试

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

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

票数 0
EN

Stack Overflow用户

发布于 2020-03-05 09:04:33

这是我在2015年解决这个问题的方法:

代码语言:javascript
运行
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28478765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档