using java.util.Scanner to read a file byte by byte
- by openidsucks
I'm trying to read a one line file character by character using java.util.Scanner. However I'm getting this exception":
Exception in thread "main" java.util.InputMismatchException: For input string: "contents of my file"
at java.util.Scanner.nextByte(Scanner.java:1861)
at java.util.Scanner.nextByte(Scanner.java:1814)
at p008.main(p008.java:18) <-- line where I do scanner.nextByte()
Here's my code:
public static void main(String[] args) throws FileNotFoundException {
File source = new File("file.txt");
Scanner scanner = new Scanner(source);
while(scanner.hasNext()) {
System.out.println((char)scanner.nextByte());
}
scanner.close()
}
Does anyone have any ideas as to what I might be doing wrong?
Edit: I realized I wrote hasNext() instead of hasNextByte(). However if I do that it doesn't print out anything.