java 使用 FileReader 从文本文件中读取一行,使用 System.out.println 似乎以 unicode 打印?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3151868/
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
Reading a line from a text file using FileReader, using System.out.println seems print in unicode?
提问by ProfessionalAmateur
Im still teaching myself Java so I wanted to try to read a text file and step 1) output it to console and step 2) write the contents to a new txt file.
我仍在自学 Java,所以我想尝试读取一个文本文件,然后步骤 1)将其输出到控制台,步骤 2)将内容写入新的 txt 文件。
Here is some code I have google'd to start with and it is reading the file, but when I output the line contents to the console I get the following (looks like its outputting in unicode or something... like every character as an extra byte associated to it....
这是我用谷歌开始的一些代码,它正在读取文件,但是当我将行内容输出到控制台时,我得到以下信息(看起来像它以 unicode 或其他格式输出......就像每个字符作为与之关联的额外字节....
?tFF□u□l□l□ □T□i□l□t□ □P□o□k□e□r□ <SNIP>
?tFF□u□l□l□ □T□i□l□t□ □P□o□k□e□r□ <SNIP>
Here is what the first line of the file looks like when I open in via notepad:
Full Tilt Poker Game #xxxxxxxxxx: $1 + $0.20 Sit & Go (xxxxxxxx), Table 1 - 15/30 - No Limit Hold'em - 22:09:45 ET - 2009/12/26
这是我通过记事本打开时文件第一行的样子:
Full Tilt Poker Game #xxxxxxxxxx: $1 + $0.20 Sit & Go (xxxxxxxx), Table 1 - 15/30 - No Limit Hold'em - 22:09:45 ET - 2009/12/26
Here is my code, do I need to specify the encoding to display txt file contents to the console? I assumed that simple text would be straight forward for java...but Im new and don't understand much about how finicky java is yet.
这是我的代码,我是否需要指定编码才能将 txt 文件内容显示到控制台?我认为简单的文本对于 Java 来说是直截了当的……但我是新手,对 Java 的挑剔程度还不太了解。
EDIT: I dont know if it matters but Im using Eclipse as my IDE currently.
编辑:我不知道这是否重要,但我目前使用 Eclipse 作为我的 IDE。
package readWrite;
import java.io.*;
public class Read {
public static void main(String args[])
{
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("C:\Users\brian\workspace\downloads\poker_text.txt"));
String line = reader.readLine();
while (line!=null) {
// Print read line
System.out.println(line);
// Read next line for while condition
line = reader.readLine();
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
} finally {
try { if (reader!=null) reader.close(); } catch (Exception e) {}
}
}
}
回答by karoberts
The ?tat the beginning appears to be a Byte Order Markfor a UTF-16encoded file.
在?t一开始似乎是Byte Order Mark一个UTF-16编码文件。
http://en.wikipedia.org/wiki/Byte_order_mark#UTF-16
http://en.wikipedia.org/wiki/Byte_order_mark#UTF-16
You might need to read the file in a different manner so Java can convert those UTF-16 characters to something your System.out can display.
您可能需要以不同的方式读取文件,以便 Java 可以将这些 UTF-16 字符转换为 System.out 可以显示的内容。
Try something like this
尝试这样的事情
FileInputStream fis = new FileInputStream("filename");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "UTF-16"));
OR
或者
Open up your text file in notepad again, and File/Save As. On the save screen (at least in windows 7) there is a pulldown with the encoding setting. Choose ANSIor UTF-8
再次在记事本中打开您的文本文件,然后选择文件/另存为。在保存屏幕上(至少在 Windows 7 中)有一个带有编码设置的下拉菜单。选择ANSI或UTF-8

