Showing posts with label BufferedWriter. Show all posts

How to append text to an existing file in Java?

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class AppendFileExample {

    public static void main(String[] args) {
        /* Content to write in File. */
        String strFileData = "This excerpt demonstrate how you can append content in existing File using Java.";

        /* Create object of File. */
        File objFile = new File("D:\\Readme.txt");

        try {
            /* Check if File exists at given location. */
            if (objFile.exists()) {
                /**
                 * Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data
                 * - fileName String The system-dependent filename.
                 * - append boolean if true, then data will be written to the end of the file rather than the beginning.
                 */
                FileWriter objFileWriter = new FileWriter(objFile.getAbsolutePath(), true);

                /* Create object of BufferedWriter. */
                BufferedWriter objBufferedWriter = new BufferedWriter(objFileWriter);

                /* Write content to File. */
                objBufferedWriter.write(strFileData);

                /* Close the BufferedWriter */
                objBufferedWriter.close();
                
                System.out.println("File modified!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Other References:
How to write file in Java?
How to read file in Java?
How to read/parse XML file in Java?

How to write file in Java?

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteFileExample {

    public static void main(String[] args) {
        /* Content to write in File. */
        String strFileData = "This excerpt demonstrate file writing in Java.";

        /* Create object of File. */
        File objFile = new File("D:\\Readme.txt");

        try {
            /* If file not exists at given location then create new File. */
            if (!objFile.exists()) {
                objFile.createNewFile();
            }

            /* Create object of FileWriter. */
            FileWriter objFileWriter = new FileWriter(objFile.getAbsolutePath());

            /* Create object of BufferedWriter. */
            BufferedWriter objBufferedWriter = new BufferedWriter(objFileWriter);

            /* Write content to File. */
            objBufferedWriter.write(strFileData);

            /* Close the BufferedWriter */
            objBufferedWriter.close();
            
            System.out.println("File created!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Other References:
How to append text to an existing file in Java?
How to read file in Java?
How to read/parse XML file in Java?