PrintWriter Class

Package: java.io

The PrintWriter class lets you write data to an output stream. Although you can connect a PrintWriter to any object that implements Writer, you’ll use it most often in conjunction with a BufferedWriter object.

CrossRef.eps The PrintWriter class is one of many Java I/O classes that use streams. For more information, see Streams (Overview).

Constructors

Constructor

Description

PrintWriter(Writer out)

Creates a print writer for the specified output writer.

PrintWriter(Writer out, boolean flush)

Creates a print writer for the specified output writer. If the second parameter is true, the buffer is automatically flushed whenever the println method is called.

Methods

Method

Description

void close()

Closes the file.

void flush()

Writes the contents of the buffer to the hard drive.

void print(value)

Writes the value, which can be any primitive type or any object. If the value is an object, the object’s toString() method is called.

void println(value)

Writes the value, which can be any primitive type or any object. If the value is an object, the object’s toString() method is called. A line break is written following the value.

Creating a PrintWriter object

Creating a PrintWriter object to write data to an output file is a bit of a convoluted process that typically involves four distinct classes. First, you must create a File object, which identifies the output file. Then you create a FileWriter to write to the file. But because the FileWriter is inefficient, you next create a BufferedWriter for more efficient output. Only then can you create a PrintWriter. Here’s the resulting code:

File file;

FileWriter fwriter;

BufferedWriter bwriter;

PrintWriter out;

file = new File(“myfile.txt”);

fwriter = new FileWriter(file);

bwriter = new BufferedWriter(fwriter);

out = new PrintWriter(bwriter);

tip.eps The PrintWriter constructor accepts an optional boolean parameter simply tells the PrintWriter class that it should tell the BufferedWriter class to flush its buffer whenever you use the println method to write a line of data. Although this option may decrease the efficiency of your program by a small amount, it also makes the program a little more reliable because it reduces the odds of losing data if your program or the whole computer crashes while unwritten data is in the buffer.

Writing to a character stream

To write data to a file connected to a PrintWriter, you use the print and println methods.

tip.eps If you’re writing writing data to a text file in a delimited format, you have to include statements that write the delimiter characters to the file. For example:

System.out.print(firstname;

System.out.print(“ ”);

System.out.println(lastname);

Here, a variable named firstname is written to the file, followed by a Tab character, followed by a variable named lastname. The lastname variable is written with the println method rather than the print method. That ends the current line.

If you prefer to be a little more efficient, you can build a string representing the entire line and then write the line all at once, as follows:

String line = firstname + “ ” + lastname;

System.out.println(line);

tip.eps If you didn’t specify the flush option when you created the PrintWriter object, you can still periodically force any data in the buffer to be written to the hard drive by calling the flush method, as follows:

out.flush();

Also, when you’re finished writing data to the file, you can close the file by calling the close method, like this:

out.close();

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset