Get your own Java server
import java.io.FileWriter;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    FileWriter myWriter = new FileWriter("filename.txt");
    myWriter.write("Hello World");
    myWriter.close();
    try {
      myWriter.write("More text");
    } catch (IOException e) {
      System.out.println("Error: the writer is closed.");
    }
  }
}

              
Error: the writer is closed.