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

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

问题描述

FindBugs报告错误:

FindBugs reports a bug:


依赖于默认编码
找到一个方法的调用,该方法将执行一个字节到字符串(或字符串到字节)转换,并假设默认平台编码是合适的。这将导致应用程序行为在平台之间变化。使用替代API并明确指定charset名称或Charset对象。

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天全站免登陆