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

public class Main {
  public static void main(String[] args) throws IOException {
    FileOutputStream output = new FileOutputStream("filename.txt");
    output.write("Hello World".getBytes());
    output.close();
    try {
      output.write('!');
    } catch (IOException e) {
      System.out.println("Error: the stream is closed.");
    }
  }
}

              
Error: the stream is closed.