Append to File in Java: Explanation & Practice

Append text to existing file

Problem summary

Append a new line to an existing file without overwriting.

Starter code

import java.io.*;

public class Main {
    public static void main(String[] args) {
        // Append "New line" to existing file
        // Print: Append successful
    }
}

Expected output and test cases

  • Line appended
    Append successful

Hints

  1. FileWriter(file, true) for append mode
  2. Second boolean parameter enables append
  3. Don't forget newline character

Related File I/O exercises

Practice all File I/O exercises · Run this idea in the Java compiler