Javatpoint Logo
Javatpoint Logo

Java Program to Append a String in an Existing File

The Java IO package offers several methods to append a string to an existing file in Java. Appending a string to a file means adding new data to the end of an existing file without overwriting or deleting the previous data.

Approach: Appending string to a file.

Appending a string to a file is a simple and effective way to modify its contents. It allows for the easy addition of new data to the end of a file without having to rewrite the entire file, which can be time-consuming and inefficient.

Algorithm

  1. Initialize the name of the file to be modified.
  2. Use a BufferedReader to read the existing content of the file and print it to the console.
    Java Program to Append a String in an Existing File
  3. Use a custom method appendStrToFile to append a new string to the file with a newline character.
  4. Use a BufferedReader again to read the modified content of the file and print it to the console. Java Program to Append a String in an Existing File
  5. Handle any possible IOException that may occur during file reading and writing.

Implementation

Filename: AppendStringToFile.java

Output:

Existing content of the file:
Java is a high-level programming language. Java is portable, robust, and dynamic, with the ability to fit the needs of all types of applications.

Modified content of the file:
Java is a high-level programming language. Java is portable, robust, and dynamic, with the ability to fit the needs of all types of applications.
WELCOME






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA