Java Reference
In-Depth Information
PrintWriter
Package: java.io
The FileOutputStream class is also in this package.
Ancestor classes:
Object
|
+--Writer
|
+--PrintWriter
public PrintWriter(OutputStream streamObject)
This is the only constructor you are likely to need. There is no constructor that accepts a fi le
name as an argument. If you want to create a stream using a fi le name, use
new PrintWriter ( new FileOutputStream( File_Name ))
When the constructor is used in this way, a blank fi le is created. If there already was a fi le named
File_Name , then the old contents of the fi le are lost. If you want instead to append new text to
the end of the old fi le contents, use
new PrintWriter( new FileOutputStream ( File_Name, true ))
(For an explanation of the argument true , see Chapter 10.)
When used in either of these ways, the FileOutputStream constructor, and so the PrintWriter
constructor invocation, can throw a FileNotFoundException , which is a kind of IOException .
If you want to create a stream using an object of the class File , you can use a File object in
place of the File_Name .
public void close()
Closes the stream's connection to a fi le. This method calls flush before closing the fi le.
public void flush()
Flushes the output stream. This forces an actual physical write to the fi le of any data that has
been buffered and not yet physically written to the fi le. Normally, you should not need to
invoke flush .
public final void print( Argument )
Same as println , except that this method does not end the line, and so the next output will be
on the same line.
public final void println( Argument )
The Argument can be a string, character, integer, fl oating-point number, boolean value, or any
combination of these, connected with + signs. The Argument can also be any object, although
it will not work as desired unless the object has a properly defi ned toString() method. The
Argument is output to the fi le connected to the stream. After the Argument has been output, the
line ends, and so the next output is sent to the next line.
 
Search WWH ::




Custom Search