Java Program to Append a String in an Existing File
In Java we can append a string in an existing file using FileWriter which has an option to open file in append mode.
Java FileWriter class is used to write character-oriented data to a file. It is character-oriented class which is used for file handling in Java. Unlike FileOutputStream class, we don’t need to convert string into byte array because it provides method to write string directly.
Note: The buffer size may be specified, or the default size may be used. A Writer sends its output immediately to the underlying character or byte stream.
Class constructor used:
FileWriter(File file, boolean append) – constructs a FileWriter object given a File object in append mode.
Class methods used:
void write(String s, int off, int len)
This method writes a portion of a String.
This method closes the stream after flushing it.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.