Java language basics: stream class (1)

Source: Internet
Author: User

1. java implements data input and output through the concept of stream, or reading and writing is easier to understand. The following four classes are the base classes of all streams, which process the reading and writing of bytes and characters respectively:
InputStream: input (read) byte stream abstract class.
OutputStream: output (write) byte stream abstract class.
Reader: an abstract class of input (read) streams.
Writer: the abstract class of the output (write) streams stream.
2. the preceding four abstract classes provide the most basic read/write methods. The input/output stream has a concept of the current position (Pos), specifying the position where the next read/write will start from the stream, when the Read or Write method is called, the Pos will be moved back at the same time, depending on the number of bytes or characters to Read and Write:

InpuStream:
Method description
Public abstract int read () reads the next byte from the stream. The method returns this byte, but if it reaches the end of the stream, it returns-1.
Public int read (byte [] B) reads multiple bytes from the stream and coexist in array B. The number of bytes to be read is determined by the length of the array, the method returns the actually read bytes. If it has reached the end,-1 is returned.
Public int read (byte [] B, int off, int len)

Read multiple bytes from the stream and coexist in array B. The number of bytes to be read is determined by len. off specifies the offset of B, and the data will be saved from this offset; the method returns the actual read bytes. If it has reached the end,-1 is returned.
Public void mark (int readlimit) marks a position. When a reset is called, the Pos will be located.
Public void reset () resets the Pos to the position specified by the last mark. The default value is 0.
Public boolean markSupported () supports tag
Public long skip (long n) skips n Bytes from Pos, which is equivalent to Pos + = n.
Public void close () closes the stream to release related system resources, such as file streams, but not all stream classes need to be closed.
How many bytes can public int available () be read, equivalent to: Size-Pos


OutStream:
Method description
Public abstract void write (int B) writes a byte to the stream
Public void write (byte [] B) writes multiple bytes to the stream
Public void write (byte [] B, int off, int len) writes multiple bytes to the stream, starting from the off offset of the array and writing a maximum of len bytes.
Public void close () closes the stream to release related system resources, such as file streams, but not all stream classes need to be closed.
Public void flush () refreshes the stream. The intent of this method is: if the stream is implemented to cache the previously written data, calling this method can force the data to be actually written to the stream.


Reader and Writer are similar to InputStream and OutStream, except that they read and write characters in units and provide several methods to read and write strings.
3. All stream classes are directly or indirectly inherited from the preceding four abstract classes and provide different functions. Below are the inheritance relationships of common stream classes:
InputStream
|-ByteArrayInputStream: byte input stream.
|-FileInputStream: file input stream
|-ObjectInputStream: Object input stream
|-PipedInputStream: pipe input stream
|-FilterInputStream: filter input streams
|-BufferedInputStream: cache input stream, which is first cached when read.
|-DataInputStream: data input stream that reads the Basic Java data type.
OutStream
|-ByteArrayOutputStream: byte output stream
|-FileOutputStream: file output stream
|-ObjectOutputStream: Object output stream
|-PipedOutputStream: pipe output stream
|-FilterOutputStream: filter the output stream
|-BufferedOutputStream: cache the input stream, which is first cached when read.
|-DataOutputStream: data input stream that reads the Basic Java data type.
Reader
|-CharArrayReader: character read stream.
|-StringReader: String read stream.
|-PipedReader: the pipeline read stream.
|-BufferedReader: cache read stream.
|-InputStreamReader: reads characters from byte streams.
Writer
|-CharArrayWriter: character writing stream.
|-StringWriter: String write stream.
|-PipedWriter: Pipeline write stream.
|-BufferedWriter: cache write stream.
|-OutputStreamWriter: write characters to byte streams.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.