Java.io.PrintWriter.clearError() Method



Description

The java.io.PrintWriter.clearError() method Clears the error state of this stream. This method will cause subsequent invocations of checkError() to return false until another write operation fails and invokes setError().

Declaration

Following is the declaration for java.io.PrintWriter.clearError() method.

protected void clearError()

Parameters

NA

Return Value

This method does not return a value.

Exception

NA

Example

The following example shows the usage of java.io.PrintWriter.clearError() method.

package com.tutorialspoint;

import java.io.*;

public class PrintWriterDemo extends PrintWriter  {

   public PrintWriterDemo(OutputStream out) {
      super(System.out);
   }

   public static void main(String[] args) {
      String s = "Hello World";

      // create a new writer
      PrintWriterDemo pw = new PrintWriterDemo(System.out);

      // write substrings
      pw.write(s, 0, 5);
      pw.write(s, 6, 5);

      // flush the writer
      pw.flush();
      
      // clear the errors, if there are any
      pw.clearError();
   }
}

Let us compile and run the above program, this will produce the following result −

HelloWorld
java_io_printwriter.htm
Advertisements