Scanner fileScanner = new Scanner(myFile); fileScanner.nextLine();
This will return the first line of text from the file and discard it, because you do not store anything in it.
To overwrite an 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 catch and handle some IOException this way. In addition, the if()... else()... needed in the while() to save any line breaks in a text file.
source share