Saturday, 16 June 2018

how remove last line from file java source code

Remove last line from file randomly access and file from java and remove  last line form file
throw java code .it is to simple code when you want to remove last line from file.here we give a
simple code that remove last line from java program.

import java.io.*;

    public class DelLstLine{
         public static void main(String[] arg){
             try{
             RandomAccessFile raf = new RandomAccessFile("RandFile.txt", "rw");
             long length = raf.length();
             System.out.println("File Length="+raf.length());
             //supposing that last line is of 8
             raf.setLength(length - 8);
             System.out.println("File Length="+raf.length());
             raf.close();
             }catch(Exception ex){
             ex.printStackTrace();
         }
     }//end of psvm
}//class ends

0 comments:

Post a Comment