每次打开或写入文件时,都会应用某种编码。但有时我们忘记了我们的 IDE(在您的情况下为 Eclipse)也有编码。
当您在引号之间键入某个文本时,它会以某种编码(即您的 IDE 的编码)显示和输入。您的假设是您的输出流(UTF-8)的编码也将保证文本以该特定编码显示。但是,我认为这里也再次应用了您的 IDE 的编码。
我建议仔细检查您对 eclipse 的编码。也许这可以解决您的问题。当然值得一试,不是吗?:)
对于全局编码设置,将以下代码添加到 eclipse.ini 文件中
-Dfile.encoding=UTF-8
编辑:
我只想添加以下内容。我执行了以下步骤作为实验。
- 我打开记事本++并创建了一个新文件
- I modified the encoding setting to UTF-8
- I copied your Russian text and pasted it in my new text file and saved it.
- Next I opened my windows console ("cmd")
- I executed the "chcp 65001" command.
- Next I printed the content of the file in my console: "type file.txt"
- Everything shows correctly.
This does not confirm much, but it does confirm the fact that DOS can do the job if the content is foreseen in the right encoding.
EDIT2:
@ka3ak It's been over 2 years, but while reading a book about Java I/O I stumbled upon the following.
System.console().printf(...)
has better support for special characters than the System.out.println(...)
method.
Since the PrintStream
just wraps around the System.out
stream, I guess you have the same limitations. I am wondering if this could have solved the problem. If it still matters, please give it a try. :)
Other posts on stackoverflow report similar things: console.writeline and System.out.println