Java扫描程序更改字符串 [英] Java Scanner Changing String
问题描述
我正在使用Scanner从文本文件中读取一些基本文件.
I'm doing some basic file reading from a text file using Scanner.
前5个条目是-
0 MR2Spyder
1 Tundra
3 Echo
3 Yaris
4 ScionxB
4 ScionxD
我通常实例化扫描仪,然后执行此操作-
I instantiate the scanner normally and then do this -
String line = scanner.nextLine();
System.out.println(line);
然后我得到此输出-
ÿþ0 M R 2 S p y d e r
哪个对我没有意义-Scanner类有问题吗?我应该使用BufferedReader吗?
Which doesn't make sense to me- is there some problem with the Scanner class? Should I be using BufferedReader?
推荐答案
您的文件使用UTF-16进行了编码...字符和标题ÿþ
之间的空格表明了这一点-它是 字节顺序标记 .请参见此处:
Your file is encoded using UTF-16... the spaces between characters and the heading ÿþ
is indicative of that -- it is the byte order mark. See here:
如果16位单元使用Little-endian顺序,则字节序列将具有
0xFF
,后跟0xFE
.该序列在文本显示中以ISO-8859-1字符ÿþ
出现,期望文本为ISO-8859-1.
if the 16-bit units use little-endian order, the sequence of bytes will have
0xFF
followed by0xFE
. This sequence appears as the ISO-8859-1 charactersÿþ
in a text display that expects the text to be ISO-8859-1.
在构建Scanner
时,必须指定.
You must specify that when constructing your Scanner
.
final Scanner scanner = new Scanner(file, "UTF-16");
这篇关于Java扫描程序更改字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!