23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> I/O<br />

BufferedReader is notable for its readLine() method that allows you to read text a line at a<br />

time. This replaces the common but deprecated readLine() method in DataInputStream.<br />

Each time you read from an unbuffered reader, there's a matching read from the underlying<br />

input stream. Therefore, it's a good idea to wrap a BufferedReader around each reader whose<br />

read() operations are expensive, such as a FileReader. For example:<br />

BufferedReader br = new BufferedReader(new FileReader("37.html"));<br />

There are two constructors. One has a default buffer size (8192 characters); the other requires<br />

the programmer to specify the buffer size:<br />

public BufferedReader(Reader in, int buffer_size)<br />

public BufferedReader(Reader in)<br />

BufferedReader overrides most of its superclass's methods, including:<br />

public int read() throws <strong>IO</strong>Exception<br />

public int read(char[] text, int offset, int length) throws <strong>IO</strong>Exception<br />

public long skip(long n) throws <strong>IO</strong>Exception<br />

public boolean ready() throws <strong>IO</strong>Exception<br />

In <strong>Java</strong> 2 and later, the two multicharacter read() methods try to completely fill the specified<br />

array or subarray of text by reading repeatedly from the underlying reader. They return only<br />

when the requested number of characters have been read, the end of the data is reached, or the<br />

underlying reader would block. This is not the case for most readers (including buffered<br />

readers in <strong>Java</strong> 1.1.x), which only attempt one read from the underlying data source before<br />

returning.<br />

BufferedReader does support marking and resetting, at least up to the length of the buffer:<br />

public boolean markSupported()<br />

public void mark(int readAheadLimit) throws <strong>IO</strong>Exception<br />

public void reset() throws <strong>IO</strong>Exception<br />

The one new method in this class is readLine():<br />

public String readLine() throws <strong>IO</strong>Exception<br />

This method returns a string that contains a line of text from a text file. \r, \n, and \r\n are<br />

assumed to be line breaks and are not included in the returned string. This method is often<br />

used when reading user input from System.in, since most platforms only send the user's<br />

input to the running program after the user has typed a full line (that is, hit the Return key).<br />

readLine() has the same problem with line ends that DataInputStream's readLine()<br />

method has; that is, the potential to hang on a lone carriage return that ends the stream. This<br />

problem is especially acute on networked connections, where readLine() should never be<br />

used.<br />

Example 15.5 uses a BufferedReader and readLine() to read all files named on the<br />

command line, line by line, and copy them to System.out. In essence it implements the Unix<br />

cat or the DOS type utility.<br />

376

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!