Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove first line of a text file in java [duplicate]

Tags:

java

Possible Duplicate:
Replace first line of a text file in Java
Java - Find a line in a file and remove

I am trying to find a way to remove the first line of text in a text file using java. Would like to use a scanner to do it...is there a good way to do it without the need of a tmp file?

Thanks.

like image 952
Butterflycode Avatar asked Nov 01 '12 13:11

Butterflycode


People also ask

How do I remove the first line from a text file in Java?

Scanner fileScanner = new Scanner(myFile); fileScanner. nextLine(); This will return the first line of text from the file and discard it because you don't store it anywhere.

How do you remove duplicate lines from a text file in Java?

C Programming from scratch- Master C ProgrammingRead each line of the file Store it in a Sting say input. Try to add this String to the Set object. If the addition is successful, append that particular line to file writer. Finally, flush the contents of the FileWriter to the output file.

How do you delete a specific line from a text file in Java?

Deleting a text line directly in a file is not possible. We have to read the file into memory, remove the text line and rewrite the edited content. Although, of course, it isn't necessary that the entire file fits into memory, as you can read and write in the same loop.

How do I change the first line of a file in Java?

Invoke the replaceAll() method on the obtained string passing the line to be replaced (old line) and replacement line (new line) as parameters. Instantiate the FileWriter class. Add the results of the replaceAll() method the FileWriter object using the append() method.


1 Answers

If your file is huge, you can use the following method that is performing the remove, in place, without using a temp file or loading all the content into memory.

public static void removeFirstLine(String fileName) throws IOException {  
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");          
     //Initial write position                                             
    long writePosition = raf.getFilePointer();                            
    raf.readLine();                                                       
    // Shift the next lines upwards.                                      
    long readPosition = raf.getFilePointer();                             

    byte[] buff = new byte[1024];                                         
    int n;                                                                
    while (-1 != (n = raf.read(buff))) {                                  
        raf.seek(writePosition);                                          
        raf.write(buff, 0, n);                                            
        readPosition += n;                                                
        writePosition += n;                                               
        raf.seek(readPosition);                                           
    }                                                                     
    raf.setLength(writePosition);                                         
    raf.close();                                                          
}         

Note that if your program is terminated while in the middle of the above loop you can end up with duplicated lines or corrupted file.

like image 74
dan Avatar answered Sep 28 '22 09:09

dan