Scanner Sc = new Scanner(new File("Input.bin"));
String s = Sc.nextLine();
fsize = Integer.parseInt(s); // Reads 4
s = Sc.nextLine();
int mapSize = Integer.parseInt(s); // Reads 3
for (int i = 0; i < mapSize; i++)
{
byte value = 0;
value = Sc.nextByte(); // Here it is throwing the exception it should have
// print the ascii of 'b' which is 98????
String key = Sc.nextLine();
key = key.trim();
dcMap.put(key, (char)value);
// System.out.println(key + " " + (char)value);
}
Input.bin文件内容:
4
3
b 0
c 10
a 11
解决方法:
java.util.Scanner用于扫描文本.
因此,当您调用Scanner#nextByte()时,它真正希望找到的是数字的文本表示形式.例如,如果在那里有“ 98”而不是“ b”,则它将98读入该字节变量.
标签:java
来源: https://codeday.me/bug/20191201/2078729.html