Java Scanner Changing String

ε祈祈猫儿з 提交于 2019-12-24 12:24:28

问题


I'm doing some basic file reading from a text file using Scanner.

The first 5 entries are this -

 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);

I then get this output -

ÿþ0 M R 2 S p y d e r 

Which doesn't make sense to me- is there some problem with the Scanner class? Should I be using BufferedReader?


回答1:


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:

if the 16-bit units use little-endian order, the sequence of bytes will have 0xFF followed by 0xFE. This sequence appears as the ISO-8859-1 characters ÿþ in a text display that expects the text to be ISO-8859-1.

You must specify that when constructing your Scanner.

final Scanner scanner = new Scanner(file, "UTF-16");


来源:https://stackoverflow.com/questions/11874272/java-scanner-changing-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!