I am having weird behavior with Scanner. It will work with a particular set of files I am using when I use the Scanner(FileInputStream)
constructor, but it won't with the Scanner(File)
constructor.
Case 1: Scanner(File)
Scanner s = new Scanner(new File("file"));
while(s.hasNextLine()) {
System.out.println(s.nextLine());
}
Result: no output
Case 2: Scanner(FileInputStream)
Scanner s = new Scanner(new FileInputStream(new File("file")));
while(s.hasNextLine()) {
System.out.println(s.nextLine());
}
Result: the file content outputs to the console.
The input file is a java file containing a single class.
I double checked programmatically (in Java) that:
- the file exists,
- is readable,
- and has a non-zero filesize.
Typically Scanner(File)
works for me in this case, I am not sure why it doesn't now.
Charset.defaultCharset()
on your system? – HindustanFile
, not withFileInputStream
). I don't know if it's related but +1 nonetheless. Wasted a good hour on this. – Haiku