A subset of the Writer hierarchy
-
Writer <<abstract>>
-
BufferedWriter
-
PrintWriter
-
OutputStreamWriter
-
FileWriter
-
-
Classes used to connect a character output stream to a file
-
PrintWriter – contains the methods for writing data to a text stream
-
BufferedWriter – creates a buffer for the stream
-
FileWriter – connects the stream to a file
-
-
Constructors of these classes:
Constructors | Throws |
PrintWriter(Writer[,booleanFlush]) | none |
BufferedWriter(Writer) | none |
FileWriter(File[,booleanAppend]) | IOException |
FileWriter(StringFileName[,booleanAppend]) | IOException |
Example 1: How to connect without a buffer (not recommended)
FileWriter fileWriter = new FileWriter("products.txt"); PrintWriter out = new PrintWriter(fileWriter);
Example 2: A more concise way to code example 1
PrintWriter out = new PrintWriter(new FileWriter("products.txt"));Example 3: How to connect to a file with a buffer
PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter("products.txt")));Example 4: How to connect for an appen operation
PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter("products.txt", true)));
Example 5: How to connect with the autoflush feature turned on
PrintWriter out = new PrintWriter( new BufferedWriter( new FileWriter("products.txt")), true);
Descriptions:
- The Writer class is an abstract class that's inheried by all of the classes the Writer hierarchy.
- By default, the output file is overwritten.
More info can be found here: http://www.j2ee.me/javase/6/docs/api/java/io/Writer.html