Java-Append Data to the End of Existing File

Append Data to the End of Existing File

The following program shows how we could append items to an existing file using the RandomAccessFile class. This program opens the file “data.da” created by previous program and then appends bytes to the end.

Program

Program Source

import java.io.RandomAccessFile;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) {
        
        try(RandomAccessFile raf = new RandomAccessFile("rand.da","rw")){
            
            raf.seek(raf.length());
            raf.writeByte(50);
            raf.writeByte(60);
            raf.writeByte(70);
            raf.writeByte(80);
            
        }catch(IOException ex){
            
            System.out.println("An I/O Error Occurred");
        }
    }
}

Leave a Comment