java 字符串扫描,Java扫描程序更改字符串

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?

解决方案

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值