java使 FileWriter FileReader 无编码格式,OutputStreamWriter InputStreamReader可设置编码格式

原文链接:https://blog.csdn.net/qq_27093465/article/details/72730796

使用Java中的 FileWriter FileReader 可以传个文件路径,然后就可以简单的实现,文件的读和写。
但是这个实现是太简单了,会有问题的。

简单的代码操作,如我的这篇博文里面的转存文件的代码就是使用这2个类来实现的。

java修改文件名-renameTo()方法的使用实例,复制一个文件或者叫转存一个文件

上面的转存代码,经过findbugs分析,有如下的提示:

具体如下:
Found reliance on default encoding: new java.io.FileWriter(String)
Reliance on default encoding
Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behaviour to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

然后,翻译一下就是下图:

FileWriter FileReader 是不带编码格式的,默认使用本机器的默认编码,那么就会因为编码问题,而bug的。

怎么让 FileWriter FileReader 他们带上编码格式呢?

还是拿转存文件的代码来修改。具体修改如下。

private static boolean copyFile(String src, String des) {
    InputStreamReader fr = null;
    OutputStreamWriter fw = null;
    try {
        fr = new InputStreamReader(new FileInputStream(src),"UTF-8");//读
        fw = new OutputStreamWriter(new FileOutputStream(des), "UTF-8");//写
        char[] buf = new char[1024];//缓冲区
        int len;
        while ((len = fr.read(buf)) != -1) {
            fw.write(buf, 0, len);//读几个写几个
        }
        return true;
    } catch (IOException e) {
        LOG.error(e.getMessage());
        return false;
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                LOG.error(e.getMessage());
            }
        }
        if (fw != null) {
            try {
                fw.flush();
                fw.close();
            } catch (IOException e) {
                LOG.error(e.getMessage());
            }
        }
    }
}

重点就是把
FileWriter FileReader 换成了 OutputStreamWriter InputStreamReader

new InputStreamReader(new FileInputStream(src),“UTF-8”);//读
new OutputStreamWriter(new FileOutputStream(des), “UTF-8”);//写

这就带上了编码格式了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值