I need to retrieve from a file always the last line written. I know one way to do this would be:
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class LineReader {
public static void main(String[] args) throws Exception {
LineNumberReader lineCounter = new LineNumberReader(new InputStreamReader(new FileInputStream("C:\MyFile.txt")));
String nextLine = null;
try {
while ((nextLine = lineCounter.readLine()) != null) {
if (nextLine == null)
break;
System.out.println(nextLine);
}
System.out.println("Total number of line in this file " + lineCounter.getLineNumber());
} catch (Exception done) {
done.printStackTrace();
}
}
}
But is there any java-ready method to get this line without having to go through all the lines in the file? Even more that I will never know how many lines he already has.