I have a simple output text file, it is called logOutput.txt
:
2
3
4
And I want my Java Program to give the most recent one in the file ( 4 ). Here is my code:
import java.util.*;
import java.io.*;
public class readLogFile {
public static void main(String[] a) {
int logNumber = 0;
try {
File file = new File("logOutput.txt");
Scanner input = new Scanner(file);
while (input.hasNext()) {
logNumber = input.nextInt();
}
System.out.println("the log # is: " + logNumber);
} catch (FileNotFoundException fne) {
}
}
}// end class
My concern is mainly regarding the while loop. Is there a way that is more efficient? Here I'm reading each line into the variable then over-writing that. I was thinking - maybe to check for when the nextInt
is null ?
thanks