I have directory with many files in it - each with over 800 lines in it. Hovewer, when I try to read it using Scanner, it seems empty.
File f1 = new File("data/cityDistances/a.txt"),
f2 = new File("data/cityDistances/b.txt");
System.out.println(f1.exists() && f2.exists()); //return true
System.out.println(f1.getTotalSpace() > 0 && f2.getTotalSpace() > 0); //return true
Scanner in = new Scanner(f1);
System.out.println(in.hasNext()); // return false;
System.out.println(in.hasNextLine()); //return false;
Why can it behave like that?
I've managed to do it using BufferedReader
. Nonetheless, it seems even more strange that BufferedReader
works and Scanner
didn't.
As the default delimeter for Scanner is whitespace, that would imply your a.txt
contains only whitespace - does it have 800 lines of whitespace? ;)
Have you tried the following?
new Scanner(new BufferedReader(new FileReader("a.txt")));
I had a similar problem today reading a file with Scanner. I specified the encoding type of the file and it solved the problem.
scan = new Scanner(selectedFile,"ISO-8859-1");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With