printWriter checkError Example
PrintWriter class checkError example. This example shows you how to use checkError method.
PrintWriter class checkError example.method returns true if it encounter any underlying outputstream or format conversion error.three streams have been used in the program.but they all were closed with .close method before there implementation with append method.hence an stream output error is caught by checkError method.
Here is the code:-
/**
* @Program uses checkError method to flush or to immediate
* rush to the result and to detect any error in underlying
* output stream.
* CheckError.java
* Author:-RoseIndia Team
* Date:-5-july-2008
*/
import java.io.*;
public class CheckError {
public static void main(String args[]) throws IOException {
File f1 = new File("/home/mahendra/Desktop/pepsi.txt");
PrintWriter out = new PrintWriter(new FileWriter(f1, true));
FileOutputStream f = new FileOutputStream(f1, true);
out.close();
out.append("theory of relativity");
System.out.println(out.checkError());
System.out.println("Method encountered stream output error.");
System.out.println("Hence boolean 'true' is returned");
}
} |
Output of the program:-
true
Method encountered stream output error.
Hence boolean true is returned |
|