java 发现依赖于 FileReader 中的默认编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11963462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:06:08  来源:igfitidea点击:

Found reliance on default encoding in FileReader

javafindbugs

提问by Mike

How can we fix the below issue reported by findBugs:

我们如何解决 findBugs 报告的以下问题:

Found reliance on default encoding in abc.java : new java.io.FileReader(File)

I'm just reading file & Findbug has reported the issue.

我只是在阅读文件,Findbug 已经报告了这个问题。

Any help is much appreciated!

任何帮助深表感谢!

采纳答案by jtahlborn

use an explicit character encoding when opening a file instead of relying on the platform default (which can change depending on the platform), unless of course, you intend to use the platform default. you can use InputStreamReaderto convert a FileInputStreamto a Readerusing an explicit character encoding.

打开文件时使用显式字符编码,而不是依赖平台默认值(可能会因平台而异),当然,除非您打算使用平台默认值。您可以使用显式字符编码InputStreamReader将 a 转换FileInputStream为 a Reader