依赖默认编码,我应该使用什么以及为什么? [英] Reliance on default encoding, what should I use and why?

查看:37
本文介绍了依赖默认编码,我应该使用什么以及为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FindBugs 报告了一个错误:

FindBugs reports a bug:

依赖默认编码找到了对将执行字节到字符串(或字符串到字节)转换的方法的调用,并假定默认平台编码是合适的.这将导致应用程序行为因平台而异.使用替代 API 并明确指定字符集名称或字符集对象.

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.

我是这样使用 FileReader 的(只是一段代码):

I used FileReader like this (just a piece of code):

public ArrayList<String> getValuesFromFile(File file){
    String line;
    StringTokenizer token;
    ArrayList<String> list = null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(file));
        list = new ArrayList<String>();
        while ((line = br.readLine())!=null){
            token = new StringTokenizer(line);
            token.nextToken();
            list.add(token.nextToken());
    ...

为了纠正这个错误我需要改变

To correct the bug I need to change

br = new BufferedReader(new FileReader(file));

br = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.defaultCharset()));

当我使用 PrintWriter 时,发生了同样的错误.所以现在我有一个问题.当我可以(应该)使用 FileReader 和 PrintWriter 时,如果不是好习惯,请依赖默认编码?而第二个问题是正确使用Charset.defaultCharset()?我决定使用这种方法来自动定义用户操作系统的字符集.

And when I use PrintWriter the same error occurred. So now I have a question. When I can (should) use FileReader and PrintWriter, if it's not good practice rely on default encoding? And the second question is to properly use Charset.defaultCharset ()? I decided use this method for automatically defining charset of the user's OS.

推荐答案

如果文件在你的应用程序的控制之下,并且如果你希望文件以平台的默认编码编码,那么你可以使用默认平台编码.明确指定它可以让您和未来的维护者更清楚这是您的意图.例如,对于文本编辑器来说,这将是一个合理的默认值,然后它将写入该平台上的任何其他编辑器都可以读取的文件.

If the file is under the control of your application, and if you want the file to be encoded in the platform's default encoding, then you can use the default platform encoding. Specifying it explicitely makes it clearer, for you and future maintainers, that this is your intention. This would be a reasonable default for a text editor, for example, which would then write files that any other editor on this platform would then be able to read.

另一方面,如果您想确保可以在文件中写入任何可能的字符,则应使用通用编码,如 UTF8.

If, on the other hand, you want to make sure that any possible character can be written in your file, you should use a universal encoding like UTF8.

如果文件来自外部应用程序,或者应该与外部应用程序兼容,那么您应该使用该外部应用程序期望的编码.

And if the file comes from an external application, or is supposed to be compatible with an external application, then you should use the encoding that this external application expects.

您必须意识到,如果您像在一台机器上那样编写文件,并像在另一台机器上那样读取它,而这台机器没有相同的默认编码,则不一定能够阅读您所写的内容.使用特定的编码进行写入和读取,例如 UTF8,确保文件始终相同,无论写入文件时使用什么平台.

What you must realize is that if you write a file like you're doing on a machine, and read it as you're doing on another machine, which doesn't have the same default encoding, you won't necessarily be able to read what you have written. Using a specific encoding, to write and read, like UTF8 makes sure the file will always be the same, whatever platform is used when writing the file.

这篇关于依赖默认编码,我应该使用什么以及为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆