Why on earth would you want to use a scanner to read a file byte by byte? That's like using a wheelbarrow to transport your pocket change. (If you really need a wheelbarrow for your pocket change, let me know so I can become your friend).
But seriously: Class InputStream
reads bytes from a file, simply and reliably, and does nothing else.
Class scanner
was recently introduced into the Java API so textbook examples could pull data out of a file with less pain than is usually involved with using the cascade of new BufferedReader(new InputStream)
. Its specialty is inputting numbers and strings from free-form input files. The nextByte()
method actually reads one or a few decimal digits from the input stream (if they're there) and converts the number thus scanned into a single byte value.
And if you're reading bytes, why do you want to output them as char
s? Bytes are not chars, and brute-force interconverting will fail in some places. If you want to see the values of those bytes, print them out as they are and you'll see small integers between 0 and 255.
If you want to read char
s from a file, FileReader
is the class for you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…