Java Program to write a file in append mode


In this program, You will learn how to write a file in append mode in java.


FileWriter fw = new FileWriter("abc.txt", true);

fw.write(str);

Example: How to write a file in append mode in java.

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

class Test {

    public static void main(String args[]) {

        String str;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter String value :");
        str = sc.nextLine();

        try {
            FileWriter fw = new FileWriter("abc.txt", true);
            fw.write(str);
            fw.close();

        } catch (IOException e) {
            System.out.println(e);
        }

        System.out.println("\nSuccessfully Done");
    }
}

Output:

Enter String value :John Smith
Successfully Done