Appending Data to the End of a File
(Adding Data to Existing File!)

Inserting data "somewhere" in a sequential file would require that the entire file be rewritten. It is possible, however, to add data to the end of a file without rewriting the entire file. Adding data to the end of an existing file is called appending.

To attempt appending a file, the FileWriter class will be needed
as it can directly address appending.
FileWriter(String file, boolean append)
If append is stated as true, output will be appended to the end of the existing file contents.

FileWriter fw = new FileWriter("YourFile.txt", true);
PrintWriter pw = new PrintWriter(fw);

catch (IOException e)
The exception on FileWriter is IOException, so the catch will address this exception.
The FileNotFoundException of PrintWriter falls under the IOException.

dividerdash

appendcode

After amending, the file NewFile.txt will now be:
Ed Carol John Sue
1 2 3 4 5 6 7 8 9 10
Yoko Jaden Alea Yung
11 12 13 14 15 16 17 18 19 20

 

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.