Appending to a File

Appending to a File

How can I append to a file in Java?The file is ASCII. It exists. The program hasrights to the file. I just need to add to the endof the file.

You need to use Java’s RandomAccessFile class to accomplish this.Once you open a file with read and write permissions, you cansimply seek to the end position of the file and write your datafrom that point using one of the various write methods availablein that class:

       public void write(byte  b[]);       public void write(byte  b[], int  off, int  len);       public void write(int  b);       public final void writeBoolean(boolean  v);       public final void writeByte(int  v);       public final void writeBytes(String  s);       public final void writeChar(int  v);       public final void writeChars(String  s);       public final void writeDouble(double  v);       public final void writeFloat(float  v);       public final void writeInt(int  v);       public final void writeLong(long  v);       public final void writeShort(int  v);       public final void writeUTF(String  str);
For example, the following program appends a string from the command lineto the end of a file:
——————- —————–import*;public class FileAppend {       //       // Defining a main() routine allows the program to be       // run as a standalone java application       //       public static void main(String argv[]) {               if (argv.length != 2) {                       System.out.println(“Usage: FileAppend string outfile”);                       System.exit(1);               }               RandomAccessFile output = null;               try {                       // Open the file for reading and writing                       output = new RandomAccessFile(argv[1], “rw”);               } catch (Exception e) {                       System.out.println(“Couldn’t open ” + argv[1]);                       System.exit(1);               }               try {                       // seek to the end of the file             ;                       // perform the write operation                       output.writeChars(argv[0] + ”
” ); } catch (Exception e) { System.out.println(“Error appending to file”); System.exit(1); } }}

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several