InputStream and OutputStream

Source: Internet
Author: User

1, in Java stream represents a kind of data flow (source), Javaio of the underlying data element,----(imagined as a faucet)

2. Any Javaio object capable of generating a data stream (source) can be regarded as a InputStream object since it can produce data, we can take the data out, Java to the generic method of encapsulation on the Read () method-(out of the faucet)
3. Any Javaio object capable of receiving a data source (stream) can be seen as a OutputStream object
Similarly, it can receive data, we can call its write method, to let it receive data-(into the faucet, hehe)
4, of course, we can on the basis of InputStream and outputstream data sources, from the actual need to trigger,
To reseal the input and output streams for different performance mechanisms, the Java.io package provides a rich range of input and output stream objects, such as:
Stream based on byte stream:
DataOutputStream----DataInputStream:
FileOutputStream-----FileInputStream:
............. And so on, can be consulted from the JDK document with InputStream and OutputStream
Stream based on character streams (typically identified by write and reader):
FileWriter---FileReader:
StringWriter---StringReader:
......... And so on, you can use writer and reader to view the instructions from the JDK document.
Stream should be the water in the faucet,
InputStream: is a kind of object that is out of the faucet (the water is encapsulated inside), the object's Read method, think of it as this
Out of the faucet this mechanism object of the switch button, you read or OpenStream (other object Containment InputStream object method), it is equal to open the faucet button, water out, inside the package of water is what nature, you use the corresponding container to install, such as String or byte[] .....
OutputStream: You're on the inputstream basis, okay?
--------------------------------------------------------------------------------------------------------------- ----------
OutputStream
(1) Output data
void write (int b) writes a byte to the stream B
void write (Byte b[]) writes a byte array to the stream b
void Write (Byte b[],int off,int len) starts byte array b from subscript off, and len-length bytes are written to the stream
(2)
Flush () brushes the empty output stream and outputs all cached bytes
Because some streams support caching, this method forces all content in the cache to be output to the stream.
(3) Close the stream
The close () flow must be closed after the operation has finished.
--------------------------------------------------------------------------------------------------------------- ---------
InputStream
(1) reading data from the stream
int read () reads a byte, the return value is the read byte
int read (byte b[]) reads multiple bytes, places it into byte array b, usually reads a number of bytes of B, and returns the value of the actual single-fetched
The number of bytes.
int read (byte b[], int off,int len) reads Len bytes, placing it to the following mark off start byte array B, the return value is the actual
The number of bytes read.
the int available () return value is the number of bytes that have not been read in the stream.
Long Skip (long n), read pointer skips n bytes not read, return value is the number of bytes actually skipped
(2) Close the stream
Close () must be closed after a stream operation has completed
(3) using markers in the input stream
void mark (int readlimit) records the position of the current pointer. Readlimit indicates that the read pointer reads the readlimit of a word
The marked pointer position is not effective.
void Reset () re-points the read pointer to the location recorded by the Mark method
Boolean marksupported () whether the current stream supports the record function of the read pointer.
--------------------------------------------------------------------------------------------------------------- --------

Java IO is implemented through stream. About the flow, it can be understood as a "pipe of data". Things that flow in a pipeline can be byte-based or character-based. It's like flowing water inside a pipe, and it can flow oil.

There is also a concept that corresponds to the flow: input, output devices. These devices can be disk files, keyboards (input devices), monitors (output devices), printers (output devices), network sockets, and so on.

Below, let's look at the flow.

Two types of streams are defined in Java: Byte-type, and character-type.

BYTE stream: Handles input and output of bytes. including reading and writing binary data, and other aspects of the content.

Character Stream: Handles the input and output of characters. He uses Unicode encoding, which allows for internationalization. Another advantage of using character streams is that character streams are more efficient than byte streams.

BYTE stream:

A byte stream contains two top-level abstract classes:InputStream and outputstream.

1: The two top-level classes of the byte stream are abstract classes, respectively:inputstream and outputstream.

2: Each abstract class has subclasses to implement specific functions to handle the input and output of different devices.

The following is a brief introduction to several common sub-categories of byte stream:::

BYTE Stream class

Function Brief Introduction

DataInputStream

Contains input streams that read Java standard data types

DataOutputStream

Contains output streams that write Java standard data types

ByteArrayInputStream

The input stream read from the byte array

ByteArrayOutputStream

Writes the output stream of a byte array

FileInputStream

Input stream read-in from file

FileOutputStream

Output stream written to file

PrintStream

Contains the most common output streams for print () and println ()

PushbackInputStream

Returns a byte to the input stream, primarily for compiler implementations

PipedInputStream

Output piping

PipedOutputStream

Input piping

SequenceInputStream

Combine n input streams, one after another, in a sequential read

Randomaccessfile

Random access to files

BufferInputStream

Buffered input stream

BufferOutputStream

Buffered output stream

FilterInputStream

Realized the inputstream Interface

FilterOutputStream

Realized the outputstream Interface

InputStream

Abstract class that describes the input of a stream

OutputStream

Abstract class that describes the input of a stream

Abstract classes InputStream and Outpurstream define practical methods, the most important of which is read () and

Write (). These two methods are declared as abstract methods in InputStream and OutputStream , and are implemented by the child stream class overwrite.

2: Each abstract class has subclasses to implement specific functions to handle the input and output of different devices.

The following is a brief introduction to several common sub-categories of byte stream:::

BYTE Stream class

Function Brief Introduction

DataInputStream

Contains input streams that read Java standard data types

DataOutputStream

Contains output streams that write Java standard data types

ByteArrayInputStream

The input stream read from the byte array

ByteArrayOutputStream

Writes the output stream of a byte array

FileInputStream

Input stream read-in from file

FileOutputStream

Output stream written to file

PrintStream

Contains the most common output streams for print () and println ()

PushbackInputStream

Returns a byte to the input stream, primarily for compiler implementations

PipedInputStream

Output piping

PipedOutputStream

Input piping

SequenceInputStream

Combine n input streams, one after another, in a sequential read

Randomaccessfile

Random access to files

BufferInputStream

Buffered input stream

BufferOutputStream

Buffered output stream

FilterInputStream

Realized the inputstream Interface

FilterOutputStream

Realized the outputstream Interface

InputStream

Abstract class that describes the input of a stream

OutputStream

Abstract class that describes the input of a stream

Abstract classes InputStream and Outpurstream define practical methods, the most important of which is read () and

Write (). These two methods are declared as abstract methods in InputStream and OutputStream , and are implemented by the child stream class overwrite.

InputStream vs OutputStream

InputStream is used for data reading, while OutputStream is used for data writing. Interface Description:

publicabstractclassInputStream implementsCloseable {    publicabstractintread() throwsIOException;    publicintread(byteb[]) throwsIOException;    publicintread(byteb[], int off, intlen) throwsIOException;    publiclongskip(longn) throwsIOException;    public voidclose() throwsIOException;    publicsynchronizedvoidmark(intreadlimit);    publicsynchronizedvoidreset() throwsIOException;    //...}publicabstractclassOutputStream implementsCloseable, Flushable {   public abstractvoidwrite(intb) throwsIOException;   publicvoidwrite(byteb[]) throwsIOException;   publicvoidwrite(byteb[], intoff, intlen) throwsIOException;   publicvoid flush() throwsIOException;   publicvoidclose() throwsIOException;}
Close Streams

The stream in Java is required to be explicitly closed by the Close method. OutputStream There is one more method called the Flush,close method automatically executes flush. Therefore, you do not need to call the Flush method before calling close.

If a stream is built by passing a constructor to another stream, the original stream is closed when the stream is close, see the following code:

OutputStream file = new FileOutputStream( "quarks.ser");OutputStream buffer = newBufferedOutputStream( file );ObjectOutput output = newObjectOutputStream( buffer );try{        output.writeObject(quarks);}finally{        output.close();}
Derived classes

If you have time, you can simply explain each derived class.

InputStream and OutputStream

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.