学习笔记,转自:https://stackoverflow.com/questions/13178397/how-to-remove-first-line-of-a-text-file-in-java

 

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();                                                          
}