ISO 8859-1 Java 程序打印文件的编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7341806/
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
ISO 8859-1 Encoding of files printed in Java program
提问by user265767
I write a program that implements a file structure, the program prints out a product file based on the structure. Product names include letters ?, ? and ?. These letters are not displayed correctly in the output file. I use
我写了一个实现文件结构的程序,程序根据结构打印出一个产品文件。产品名称包括字母 ?, ? 和 ?。这些字母在输出文件中未正确显示。我用
PrintWriter printer = new PrintWriter(new FileOutputStream(new File("products.txt")));
IS0 8859 - 1 or Windows ANSI (CP 1252) is the character sets that the implementation requiers.
IS0 8859 - 1 或 Windows ANSI (CP 1252) 是实现所需的字符集。
回答by Stephen C
There are two possibilities:
有两种可能:
- Java is using the wrong encoding when outputting the file.
- The file is actually correct, and whatever you are using to display the file is using the wrong encoding.
- Java 在输出文件时使用了错误的编码。
- 该文件实际上是正确的,无论您使用什么来显示文件,都使用了错误的编码。
Assuming that the problem is the first one, the root cause is that Java has figured out that the default encoding for the platform is something other than the one you want / expect. There are three ways to solve this:
假设问题是第一个,根本原因是 Java 已经发现平台的默认编码不是您想要/期望的编码。有三种方法可以解决这个问题:
Figure out whyJava has the got default locale and encoding "wrong" and remedy that. It will be something to do with your operating system's locale settings ...
Read this FAQfor details on how you can override the default locale settings at the command line.
Use a
PrintWriter
constructor that specifies the encoding explicitly so that your application doesn't rely on the default encoding. For example:PrintWriter pw = new PrintWriter("filename", "ISO-8859-1");
找出为什么Java 有默认的语言环境和编码“错误”并解决这个问题。这将与您的操作系统的区域设置有关...
阅读此常见问题解答以了解有关如何在命令行中覆盖默认区域设置的详细信息。
使用
PrintWriter
明确指定编码的构造函数,以便您的应用程序不依赖于默认编码。例如:PrintWriter pw = new PrintWriter("filename", "ISO-8859-1");
In response to this comment:
对此评论的回应:
Don't PrintWriters all have the bug that you can't know you had an error with them?
难道 PrintWriters 都存在您不知道他们有错误的错误吗?
- It is not a bug, it is a design feature.
- You can find out if there was an error. You just can't find out what it was.
- If you don't like it, you can use
Writer
instead.
- 这不是错误,而是设计功能。
- 您可以查明是否有错误。你只是无法找出它是什么。
- 如果你不喜欢它,你可以用它
Writer
代替。
They won't raise an exception or even return failure if you try to shove a codepoint at them that can't fit in the designated encoding.
如果您尝试向它们推送不适合指定编码的代码点,它们将不会引发异常甚至返回失败。
Neither will a regular Writer
I believe ... unless you specifically construct it to do this. The normal behaviour is to replace any unmappable codepoint with a specific character, though this is not specified in the javadocs (IIRC).
Writer
我相信普通人也不会......除非你专门构建它来做到这一点。正常行为是用特定字符替换任何不可映射的代码点,尽管这在 javadocs (IIRC) 中没有指定。
Do they even tell if you the filesystem fills up; I seem to recall that they don't.
他们甚至会告诉您文件系统是否已满?我似乎记得他们没有。
That is true. However:
那是真实的。然而:
For the kind of file you typically write using a
PrintWriter
this is not a critical issue.If it is a critical issue AND you still want to use
PrintWriter
, you can always callcheckError()
(IIRC) to find out if there was an error.
对于您通常使用 a 编写的文件类型,
PrintWriter
这不是一个关键问题。如果这是一个关键问题并且您仍然想使用
PrintWriter
,您可以随时调用checkError()
(IIRC) 以查明是否存在错误。
I always end up writing my out OutputStreamWriter constructor with the explicit Charset.forName("UTF-8").newEncoder() second argument. It's kind of tedious, so perhaps there's a better way.
我总是最终用显式的 Charset.forName("UTF-8").newEncoder() 第二个参数写出我的 OutputStreamWriter 构造函数。这有点乏味,所以也许有更好的方法。
I dunno.
我不知道。