从文本文件中读取的第一个字符: [英] First character of the reading from the text file : 
问题描述
如果我写这个代码,我得到这个输出 - >第一:
然后其他行
If I write this code, I get this as output --> This first:  and then the other lines
try {
BufferedReader br = new BufferedReader(new FileReader(
"myFile.txt"));
String line;
while (line = br.readLine() != null) {
System.out.println(line);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
如何避免?
推荐答案
你在第一行得到字符因为这个序列是 UTF-8字节顺序标记(BOM)。如果文本文件以BOM开头,则可能是由记事本等Windows程序生成的。
You are getting the characters  on the first line because this sequence is the UTF-8 byte order mark (BOM). If a text file begins with a BOM, it's likely it was generated by a Windows program like Notepad.
为了解决您的问题,我们选择将文件显式地读为UTF -8,而不是任何默认系统字符编码(US-ASCII等):
To solve your problem, we choose to read the file explicitly as UTF-8, instead of whatever default system character encoding (US-ASCII, etc.):
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream("myFile.txt"),
"UTF-8"));
然后在UTF-8中,字节序列解码为一个字符,即U + FEFF。此字符是可选的 - 合法的UTF-8文件可以或不可以开头。因此,只有当它是U + FEFF时,才跳过第一个字符:
Then in UTF-8, the byte sequence  decodes to one character, which is U+FEFF. This character is optional - a legal UTF-8 file may or may not begin with it. So we will skip the first character only if it's U+FEFF:
in.mark(1);
if (in.read() != 0xFEFF)
in.reset();
现在,您可以继续使用其余代码。
And now you can continue with the rest of your code.
这篇关于从文本文件中读取的第一个字符:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!