I have the following code:
Scanner scanner = new Scanner (file);
while (scanner.hasNextLine()){
System.out.println ("Next line");
String line = scanner.nextLine();
}
At first the print statement never executed. I thought that was odd because the file is an xml file with about 2,000 lines. For kicks I tried inserting the following before initializing the Scanner object:
try{
Desktop.getDesktop().open(file);
} catch (Exception e){}
When I ran the program, the file was opened AND the print statement executed all 2,000 times (meaning scanner.hasNextLine() worked). I thought it was an issue pertaining to permissions so commented out the code above and I tried:
file.setWritable(true);
file.setExecutable(true);
file.setReadable(true);
scanner.hasNextLine() went back to not working and the print statement was never executed. What am I doing wrong?
UPDATE:
entire function looks like this:
public void addToList (File file){
try{
file.setWritable(true);
file.setExecutable(true);
file.setReadable(true);
if (file.exists())
System.out.println("File exists");
Scanner scanner = new Scanner (file, "UTF-8");
int lineCount=0;
while (scanner.hasNextLine()){
System.out.println("Next line");
String line = scanner.nextLine();
}
}
catch (FileNotFoundException e){
System.err.println (e.getMessage());
}
}
where file is an absolute path