java - Read last line from a file -
so trying read file in java. works fine, unless last line empty, in case gets ignored; need read empty line too.
here code:
try { bufferedreader in = new bufferedreader(new filereader("filename.txt")); string line; while((line = in.readline()) != null) { system.out.println("l| " + line); } } catch(exception e){e.printstacktrace();} }
first use scanner class...as hum easier use....and store each line in list , last line..here code:
public void readlast()throws ioexception{ filereader file=new filereader("e:\\testing.txt"); //address of file list<string> lines=new arraylist<>(); //to store lines scanner sc=new scanner(file); while(sc.hasnextline()){ //checking presence of next line lines.add(sc.nextline()); //reading , storing lines } sc.close(); //close scanner system.out.print(lines.get(lines.size()-1)); //displaying last one.. }
Comments
Post a Comment