Reputation: 507
Scanner scanner= new Scanner(new File("target.txt"));
and
FileInputStream d = new FileInputStream("target.txt");
What is the difference between Scanner.nextByte()
and FileInputStream.read()
?
I am trying to understand it because when i read bytes (one by one) from a file with simple text with the FileInputStream
it works fine. But when iam using Scanner
the scanner.nextByte()
doesn't return anything?
Why is that?
Upvotes: 3
Views: 11599
Reputation: 30436
The Scanner.nextByte() will read the next token and if it can be evaluated as a byte then return it while FileInoutStream.read() will return each byte of a file. Consider this example:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SO {
public static void scanner() throws FileNotFoundException {
System.out.println("Reading with the Scanner Class:");
Scanner scanner= new Scanner(new File("target.txt"));
while(scanner.hasNext()) {
try {
System.out.println("A Byte:"+scanner.nextByte());
} catch(InputMismatchException e) {
System.out.println("Not a byte:"+scanner.next());
}
}
scanner.close();
}
public static void stream() throws IOException {
System.out.println("Reading with the FileInputStream Class:");
FileInputStream d = new FileInputStream("target.txt");
int b = -1;
while((b = d.read()) != -1) {
System.out.print((byte)b+" ");
}
d.close();
System.out.println();
}
public static void main(String...args) throws IOException {
scanner();
stream();
}
}
With this as the contents of target.txt
:
Next up is a byte:
6
Wasn't that fun?
This will produce the following output:
Reading with the Scanner Class:
Not a byte:Next
Not a byte:up
Not a byte:is
Not a byte:a
Not a byte:byte:
A Byte:6
Not a byte:Wasn't
Not a byte:that
Not a byte:fun?
Reading with the FileInputStream Class:
78 101 120 116 32 117 112 32 105 115 32 97 32 98 121 116 101 58 10 54 10 87 97 115 110 39 116 32 116 104 97 116 32 102 117 110 63
Upvotes: 4
Reputation: 1602
The Scanner.nextByte()
is not the same as FileInputStream.read().
The nextByte()
method scans the next token of the input as a byte. This method will throw InputMismatchException if the next token cannot be translated into a valid byte value as described below. If the translation is successful, the scanner advances past the input that matched.
If the next token matches the Integer
regular expression defined above then the token is converted into a byte value as if by removing all locale specific prefixes, group separators, and locale specific suffixes, then mapping non-ASCII digits into ASCII digits via Character.digit, prepending a negative sign (-) if the locale specific negative prefixes and suffixes were present, and passing the resulting string to Byte.parseByte
with the specified radix.
i.e. The nextByte()
method try to match a textual representation of a number to store it in as a byte value.
On the other side, FileInputStream.read()
will read a byte of data from the input stream.
References : FileInputStream.read() Scanner.nextByte() Scanner.nextByte(int radix)
Upvotes: 1
Reputation: 10945
These classes are actually doing very different things.
A FileInputStream
is actually reading raw bytes from the input file, whereas Scanner
is parsing the file as whitespace-separated tokens, and trying the convert each token into the requested type when you ask for it.
If, for instance, your input file looks like this:
1
FileInputStream.read()
will evaluate the 1
as a byte, and return its value: 49
.
Scanner.nextByte()
will read the 1
and try to evaluate it as an integer regular expression of radix 10, and give you: 1
.
If, on the other hand, your input file contains
a
Then FileInputStream.read()
will evaluate the a
as a byte, and return its value: 97
.
Scanner.nextByte()
will read the a
and try to evaluate it as an integer regular expression of radix 10, and throw a java.util.InputMismatchException
.
Upvotes: 4