Inputstream and outputstream in Java

Source: Internet
Author: User
Inputstream and outputstream in Java

I have been working on Java for several months and have encountered many trivial technical points that have not been recorded. "Do not be small but not". It's just a bit empty today. Let's start with two streams.

Inputstream vs outputstream

Inputstream is used to read data, while outputstream is used to write data. Interface Description:

public abstract class InputStream implements Closeable {    public abstract int read() throws IOException;    public int read(byte b[]) throws IOException;    public int read(byte b[], int off, int len) throws IOException;    public long skip(long n) throws IOException;    public void close() throws IOException;    public synchronized void mark(int readlimit);    public synchronized void reset() throws IOException;    //...}public abstract class OutputStream implements Closeable, Flushable {   public abstract void write(int b) throws IOException;   public void write(byte b[]) throws IOException;   public void write(byte b[], int off, int len) throws IOException;   public void flush() throws IOException;   public void close() throws IOException;}
Close streams

Stream in Java needs to be explicitly closed through the close method. Outputstream also has a method named flush. The close method will automatically execute flush. Therefore, you do not need to call the flush method before calling close.

If a stream is imported to another stream through the constructor during construction, when the stream is closed, the original stream will also be closed. See the following code:

OutputStream file = new FileOutputStream( "quarks.ser" );OutputStream buffer = new BufferedOutputStream( file );ObjectOutput output = new ObjectOutputStream( buffer );try{        output.writeObject(quarks);}finally{        output.close();}
Derived class

 

If you have time, you will briefly explain each derived class.

 

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.