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.