Java Reference
In-Depth Information
// Put a buffered wrappter around it
BufferedWriter buf - writer =
new BufferedWriter (file - writer);
// In turn, wrap the PrintWriter stream around this
// output stream and turn on the autoflush.
PrintWriter print - writer =
new PrintWriter (buf - writer, true);
// Use the PrintWriter println methods to send
// strings and primitives to the file.
print - writer.println ( " Text output with PrintWriter. " );
print - writer.println (
" Primitives converted to strings: " );
boolean a - boolean = false;
byte a - byte = 114;
short a - short = 1211;
int an - int = 1234567;
long a - long = 987654321;
float a - float = 983.6f;
double a - double = -4.297e-15;
print - writer.println (a - boolean);
print - writer.println (a - byte);
print - writer.println (a - short);
print - writer.println (an - int);
print - writer.println (a - long);
print - writer.println (a - float);
print - writer.println (a - double);
// PrintWriter doesn't throw IOExceptions but instead
// offers the catchError () method
if (print - writer.checkError ()) {
System.out.println ( " An output error occurred! " );
}
}
catch (IOException ioe) {
System.out.println ( " IO Exception " );
}
} // main
} // class PrintWriterFileApp
Running this code produces a file that holds text exactly like that shown as the
output from PrintWriterApp in Section 9.4.1.
 
Search WWH ::




Custom Search