Friday, December 29, 2017

Some times we need to append text into File in Java instead of creating
new File. Thankfully Java File API is very rich and it provides several ways to
append text into File in Java. Previously we have seen how
to create file and directory in Java and how
to read and write to text file in Java and in this Java IO tutorial we will
see how to append text into file in Java. We are going to use standard FileWriter and BufferedWriter approach
to append text to File. One of the key point to remember while using FileWriter in Java is
to initialize FileWriter to append text i.e. writing bytes
at the end of File rather than writing on beginning of the File. In next
section we will see complete Java program to append text into File in Java. By the way you can also use FileOutputStream instead of
FileWriter if you would like to write bytes, instead of text. Similar
to FileWriter, FileOutputStreamconstructor
also takes a boolean append argument to open connection to append bytes into
File in Java.

Java program to append text
to File in Java using FileWriter

In this section we will see a fully functional Java program which appends
text at the end of File. In this program, we have used FileWriter and
initialized it will append as true. For better performance we have
wrapped FileWriter into a BufferedWriter,Similar to wrapping
FileInputStream into BufferedReader to read
File line by line in Java. Since this is a test program we
have not finally block to close connection and also instead of handling
Exception we have thrown them to keep the code simple and readable. In production
code, you should always close File connection on finally
block and ready to handle FileNotFoundException and IOException in Java.

//Don't forget to
close Streams or Reader to free FileDescriptor associated with it
bufferFileWriter.close();

System.out.println("Java Program for appending content into File has been
completed");

}

}

Output:
Original File Content: This data is before any text appended into file.ModifiedFile Content: This data is
before any text appended into file.This text
should be appended in File form Java Program You can also append
contents or bytes by using FileOutputStream, FileOutputStream(String path, boolean
append) takes a boolean
parameter to append into File, which will ensure that new bytes will be written at the end of File
rather than at beginning of File.

That’s all on how to append text to File in Java. It's rather simple Java program with only
one thing to keep in mind, initializing FileWriter with
boolean append as true, so that FileWriter writes
content at the end of file instead start of the File.

@Unknown, are you asking about XLS file which has concept of row and column? or do you mean just at nth position? If later is the case then its not supported by FileWriter or PrintWriter, you need to use RandomAccess file, which supports random access by allowing you to move file pointer to read and write at arbitrary position.

If you read the article closely, you will find that similar to FileWriter, FileOutputStream constructor also takes a boolean parameter to open file in append mode. In this mode you can append new content at the end of file without losing old data.