Re: PrintWriter Question
Mark Space wrote:
Danger_Duck wrote:
Continuing my vein of simple java questions...For the
Java.io.PrintWriter, what is the difference between write and print?
class PrintTest {
public static void main( String ... args ) {
char [] c = { 42 };
PrintWriter pw = new PrintWriter( new OutputStreamWriter(
System.out ) );
pw.print( c );
pw.write( c );
pw.flush();
}
}
compile:
run:
**
BUILD SUCCESSFUL (total time: 0 seconds)
Both print '*', so I dunno. Possibly whoever extended the Writer
class to make the PrintWriter class felt there should be methods
named "print" not "write" so they just added them, even if there's no
functional difference.
The print() methods are not declared as throwing IOExceptions, which the
write() methods are. In fact, neither of them actually does (an IOException
sets an error flag which can be checked with checkError(), but then returns
normally. This makes the print() methods more convenient to use when
IOExceptions needn't be handled immediately, e.g. when writing to stdout.