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 () Read 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) Multiple bytes are read from the stream and coexist in array B. The number of bytes to be read is determined by the length of the array, and the actually read bytes are returned by the method, -1 will be returned if it has reached the end.
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) Mark a position. The pos will be located when a reset is called.
Public void reset () Reset POs to the position specified by the last mark. The default value is 0.
Public Boolean marksupported () Flag supported or not
Public long SKIP (long N) Skipping n Bytes from POS is equivalent to POS + = n.
Public void close () Close the stream to release related system resources, such as file streams, but not all stream classes need to be closed.
Public int available () How many bytes can be read, equivalent to: Size-pos
  • Outstream:

Method Description
Public abstract void write (int B) Write a byte to the stream
Public void write (byte [] B) Write multiple bytes to the stream
Public void write (byte [] B, int off, int Len) Write multiple bytes to the stream, starting from the off offset of the array, and writing a maximum of len bytes.
Public void close () Close the stream to release related system resources, such as file streams, but not all stream classes need to be closed.
Public void flush () To refresh the stream, the intent is to call this method to forcibly write data to the stream if the stream is implemented by caching the previously written data.
  • 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: Media transcoding queue input stream |-filterinputstream: filter input stream |-bufferedinputstream: cache input stream, first cached when read data. |-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 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.