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 />

This read() method returns the number of characters actually read. As with input streams<br />

reading bytes, there may not be as many characters available as you requested. Also like the<br />

read() method of an input stream, it returns -1 when it detects the end of the data.<br />

This read() method is abstract. Concrete subclasses that read bytes from some source must<br />

override this method. An <strong>IO</strong>Exception may be thrown if the underlying stream's read()<br />

method throws an <strong>IO</strong>Exception or an encoding error is detected.<br />

You can also fill an array with characters using this method:<br />

public int read(char[] buffer) throws <strong>IO</strong>Exception<br />

This is equivalent to invoking read(buffer, 0, buffer.length). Thus, it also returns the<br />

number of characters read and throws an <strong>IO</strong>Exception when the underlying stream throws an<br />

<strong>IO</strong>Exception or when an encoding error is detected. The following method reads a single<br />

character and returns it:<br />

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

Although an int is returned, this int is always between and 65,535 and may be cast to a char<br />

without losing information. All three read() methods block until some input is available, an<br />

I/O error occurs, or the end of the stream is reached.<br />

You can skip a certain number of characters. This method also blocks until some characters<br />

are available. It returns the number of characters skipped or -1 if the end of stream is reached.<br />

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

The ready() method returns true if the reader is ready to be read from, false if it isn't.<br />

Generally, this means the underlying stream has available data.<br />

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

This is not quite the same as InputStream's available() method. available() returns an<br />

int specifying how many bytes are available to be read. However, it's not always possible to<br />

tell how many characters are available in a stream without actually reading them, particularly<br />

with encodings that use characters of different widths (such as UTF-8, where a character may<br />

be one, two, or three bytes).<br />

Readers may or may not support marking and resetting, like input streams. The<br />

markSupported() method returns true if the underlying stream supports marking and<br />

resetting, false if it doesn't.<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 close() method closes the reader and its underlying input stream and releases any<br />

resources the reader held:<br />

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

364

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

Saved successfully!

Ooh no, something went wrong!