Java io reader read all about it

Class Reader

Abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

Field Summary

Constructor Summary

Method Summary

Reads all characters from this reader and writes the characters to the given writer in the order that they are read.

Methods declared in class java.lang.Object

Field Details

lock

The object used to synchronize operations on this stream. For efficiency, a character-stream object may use an object other than itself to protect critical sections. A subclass should therefore use the object in this field rather than this or a synchronized method.

Constructor Details

Reader

Reader

Method Details

nullReader

Returns a new Reader that reads no characters. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect. While the stream is open, the read() , read(char[]) , read(char[], int, int) , read(CharBuffer) , ready() , skip(long) , and transferTo() methods all behave as if end of stream has been reached. After the stream has been closed, these methods all throw IOException . The markSupported() method returns false . The mark() and reset() methods throw an IOException . The object used to synchronize operations on the returned Reader is not specified.

Читайте также:  How to active java

read

Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed.

read

Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. Subclasses that intend to support efficient single-character input should override this method.

read

Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. If the length of cbuf is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf .

read

Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf .

skip

Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached. If the stream is already at its end before this method is invoked, then no characters are skipped and zero is returned.

ready

markSupported

Tells whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override this method.

mark

Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input streams support the mark() operation.

reset

Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().

close

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

transferTo

Reads all characters from this reader and writes the characters to the given writer in the order that they are read. On return, this reader will be at end of the stream. This method does not close either reader or writer. This method may block indefinitely reading from the reader, or writing to the writer. The behavior for the case where the reader and/or writer is asynchronously closed, or the thread interrupted during the transfer, is highly reader and writer specific, and therefore not specified. If an I/O error occurs reading from the reader or writing to the writer, then it may do so after some characters have been read or written. Consequently the reader may not be at end of the stream and one, or both, streams may be in an inconsistent state. It is strongly recommended that both streams be promptly closed if an I/O error occurs.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Java IO: Readers and Writers

The Java Reader ( java.io.Reader ) and Java Writer class ( java.io.Writer ) in Java IO work much like the InputStream and OutputStream with the exception that Reader and Writer are character based. They are intended for reading and writing text. The InputStream and OutputStream are byte based, remember?

Reader

The Java Reader is the base class of all Reader ‘s in the Java IO API. Subclasses include a BufferedReader , PushbackReader , InputStreamReader , StringReader and several others.

Here is a simple Java IO Reader example:

Reader reader = new FileReader(«c:\\data\\myfile.txt»); int data = reader.read(); while(data != -1)

Notice, that while an InputStream returns one byte at a time, meaning a value between 0 and 255 (or -1 if the stream has no more data), the Reader returns a char at a time, meaning a value between 0 and 65535 (or -1 if the stream has no more data). This does not necessarily mean that the Reader reads two bytes at a time from the source it is connected to. It may read one or more bytes at a time, depending on the encoding of the text being read.

Combining Readers With InputStreams

A Java Reader can be combined with an InputStream . If you have an InputStream and want to read characters from it, you can wrap it in an InputStreamReader . Pass the InputStream to the constructor of the InputStreamReader like this:

Reader reader = new InputStreamReader(inputStream);

In the constructor you can also specify what character set to use to decode the text etc. More on that in the text on InputStreamReader .

Writer

The Java Writer class is the base class of all Writer s in the Java IO API. Subclasses include BufferedWriter and PrintWriter among others.

Here is a simple Java IO Writer example:

Writer writer = new FileWriter("c:\\data\\file-output.txt"); writer.write("Hello World Writer"); writer.close();

Combining Writers With OutputStreams

A Java Writer can be combined with an OutputStream just like Readers and InputStream ‘s. Wrap the OutputStream in an OutputStreamWriter and all characters written to the Writer are passed on to the OutputStream . Here is an OutputStreamWriter example:

Writer writer = new OutputStreamWriter(outputStream);

Combining Readers and Writers

Just like with streams, Reader ‘s and Writer ‘s can be combined into chains to achieve more interesting IO. It works just like combining the Reader with InputStream ‘s or the Writer with OutputStream ‘s. For instance, you can achieve buffering by wrapping a Reader in a BufferedReader , or a Writer in a BufferedWriter . Here are two such examples:

Reader reader = new BufferedReader(new FileReader(. )); Writer writer = new BufferedWriter(new FileWriter(. ));

Источник

Java io reader read all about it

Abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

Field Summary

Constructor Summary

Method Summary

Methods inherited from class java.lang.Object

Field Detail

lock

The object used to synchronize operations on this stream. For efficiency, a character-stream object may use an object other than itself to protect critical sections. A subclass should therefore use the object in this field rather than this or a synchronized method.

Constructor Detail

Reader

Reader

Method Detail

read

public int read(CharBuffer target) throws IOException

Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed.

read

Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. Subclasses that intend to support efficient single-character input should override this method.

read

Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

read

Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

skip

Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.

ready

markSupported

public boolean markSupported()

Tells whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override this method.

mark

Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input streams support the mark() operation.

reset

Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().

close

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью