1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.io.StringWriter;
  4. public class FlushHeaders {
  5. public static void flushHeaders(PrintWriter out, String errorMessage) {
  6. // Create a StringWriter to capture the output
  7. StringWriter sw = new StringWriter();
  8. // Create a PrintWriter to write to the StringWriter
  9. PrintWriter pw = new PrintWriter(sw);
  10. // Print the error message to the StringWriter
  11. pw.println("Error: " + errorMessage);
  12. // Flush the PrintWriter to ensure the output is written to the buffer
  13. pw.flush();
  14. // Get the error message from the StringWriter
  15. String errorOutput = sw.toString();
  16. // Print the error message to standard output
  17. System.err.println(errorOutput);
  18. }
  19. public static void main(String[] args) throws IOException {
  20. // Example usage
  21. PrintWriter out = new PrintWriter(System.out); // Get a PrintWriter to System.out
  22. flushHeaders(out, "Simulated error during header processing.");
  23. out.println("Continuing execution...");
  24. }
  25. }

Add your comment