How to remove first line of a text file in java

java

Solution

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.

To overwrite your existing file:

FileWriter fileStream = new FileWriter("my/path/for/file.txt");
BufferedWriter out = new BufferedWriter(fileStream);
while(fileScanner.hasNextLine()) {
    String next = fileScanner.nextLine();
    if(next.equals("\n")) 
       out.newLine();
    else 
       out.write(next);
    out.newLine();   
}
out.close();

Note that you will have to be catching and handling some `IOException`s this way. Also, the `if()... else()...` statement is necessary in the `while()` loop to keep any line breaks present in your text file.

Problem

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.

Original source

Related problems