Java-Scanner 不适用于 nextByte()

Sar*_*raz 4 java

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);
}
Run Code Online (Sandbox Code Playgroud)

Input.bin文件内容:

4
3
b 0
c 10
a 11
Run Code Online (Sandbox Code Playgroud)

mak*_*mov 5

java.util.Scanner用于扫描文本

因此,当您调用 时Scanner#nextByte(),它真正期望找到的是数字的文本表示。例如,如果您在那里使用“98”而不是“b”,它会将 98 读入该字节变量。