io - how to read last line in a text file using java -
this question has answer here:
- quickly read last line of text file? 7 answers
i making log , want read last line of log.txt file, i'm having trouble getting bufferedreader stop once last line read.
here's code:
try { string scurrentline; br = new bufferedreader(new filereader("c:\\testing.txt")); while ((scurrentline = br.readline()) != null) { system.out.println(scurrentline); } } catch (ioexception e) { e.printstacktrace(); } { try { if (br != null)br.close(); } catch (ioexception ex) { ex.printstacktrace(); } }
here's solution.
in code, create auxiliary variable called lastline
, reinitialize current line so:
string lastline = ""; while ((scurrentline = br.readline()) != null) { system.out.println(scurrentline); lastline = scurrentline; }
Comments
Post a Comment