[Java I/O] byte stream InputStream/OutputStream, inputstream byte stream
Byte stream InputStream/OutputStream
This article will give a brief summary of the byte stream InputStream/OutputStream in the java I/O Stream:
In general, each byte stream class has a corresponding purpose, as shown below:
- ByteArrayInputStream/ByteArrayOutputStream // byte array
- FileInputStream/FileOutputStream // File Operations
- PipedOutputStream/PipedInputStream // inter-thread communication (pipeline)
- BufferedInputStream/BufferedOutputStream // describe other byte streams and add the buffer function
- FilterInputStream/FilterOutputStream // describe other byte streams and add some filtering processing.
- PushbackInputStream // describe other byte streams and add the rollback READ function.
- DataOutputStream/DataInputStream // describe other byte streams and add the read/write function for the basic data types of JAVA
- PrintStream // decorate other byte streams and add the formatting Data Writing Function
- ObjectInputStream/ObjectOutputStream // describe other byte streams, increase Object serialization, and read/write data to a local file in bytes
- SequenceInputStream // connects several bytes of input stream in serial form
Let's look at the composition of the byte stream InputStream/OutputStream, for example;
Next we will introduce them one by one;
ByteArrayInputStream/ByteArrayOutputStream
To put it simply, there are various conversions between byte arrays and byte input and output streams. An example is as follows:
Package io; import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; public class IOTest {public static void main (String [] args) throws IOException {// byte array in memory [] bArr = new byte [] {1, 2, 3 }; // byte input stream InputStream is = new ByteArrayInputStream (bArr); // ByteArrayOutputStream bos = new ByteArrayOutputStream (); byte [] bff = new byte [3]; // read the byte is from the input stream. read (bff, 0, 3); System. out. println (bff [0] + "," + bff [1] + "," + bff [2]); // write a byte array bos to the byte output stream. write (bff); // obtain the byte array byte [] bArryFromOs = bos from the output stream. toByteArray (); System. out. println (bArryFromOs [0] + "," + bArryFromOs [1] + "," + bArryFromOs [2]); is. close ();
Bos. close ();}}
FileInputStream/FileOutputStream
You can use FileInputStream/FileOutputStream to write and read files. The following code allows you to copy files:
Package io; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; public class IOTest {public static void main (String [] args) throws IOException {// byte input stream InputStream is = new FileInputStream ("C: \ test.jpg "); // byte output stream OutputStream OS = new FileOutputStream ("C: \ copy_test.jpg", false); int bTmp =-1; while (( BTmp = is. read ())! =-1) {OS. write (bTmp) ;}is. close (); OS. close ();}}
PipedOutputStream/PipedInputStream
It can be used for communication between two threads in the same JVM and byte stream transmission. For example:
Package io; import java. io. IOException; import java. io. pipedInputStream; import java. io. pipedOutputStream; import java. util. concurrent. executorService; import java. util. concurrent. executors; public class IOTest {public static void main (String [] args) throws IOException {final PipedOutputStream pos = new PipedOutputStream (); final PipedInputStream pis = new PipedInputStream (pos ); executorService es = Executors. newFixedThreadPool (2); es.exe cute (new Runnable () {@ Override public void run () {try {byte [] bArr = new byte [] {1, 2, 3}; pos. write (bArr); pos. close ();} catch (IOException e) {e. printStackTrace () ;}}); es.exe cute (new Runnable () {@ Override public void run () {byte [] bArr = new byte [3]; try {// will cause thread blocking pis. read (bArr, 0, 3); pis. close ();} catch (IOException e) {e. printStackTrace ();} System. out. println (bArr [0] + "," + bArr [1] + "," + bArr [2]) ;}}
BufferedInputStream/BufferedOutputStream
Byte stream with buffer, an application of the decorator mode in jdk, which can be used to describe other byte streams;
Read/write a large byte to the buffer at a time to avoid frequent access to external media and improve performance;
The following is an example:
Package io; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; public class IOTest {public static void main (String [] args) throws IOException {// byte input stream BufferedInputStream is = new BufferedInputStream (new FileInputStream ("C: \ test.jpg "), 10*1024); // byte output stream BufferedOutputStream OS = new B UfferedOutputStream (new FileOutputStream ("C: \ copy_test.jpg", false); int bTmp =-1; while (bTmp = is. read ())! =-1) {OS. write (bTmp) ;}is. close (); OS. close ();}}
FilterInputStream/FilterOutputStream
It can be used to describe other byte streams and customize the base class for filtering byte streams. Basically, it simply overwrites the methods in InputStream/OutputStream, which is not of great significance;
PushbackInputStream
Other byte input streams can be decorated. The added function is to roll back and read data, as shown in the following example:
Package io; import java. io. byteArrayInputStream; import java. io. IOException; import java. io. pushbackInputStream; public class IOTest {public static void main (String [] args) throws IOException {byte [] bArr = new byte [] {1, 2, 3 }; // byte input stream PushbackInputStream pis = new PushbackInputStream (new ByteArrayInputStream (bArr); System. out. println (pis. read (); System. out. println (pis. read (); // roll back pis. unread (1); System. out. println (pis. read (); pis. close ();}}
DataOutputStream/DataInputStream
Other byte input streams can be decorated, and the function of reading Basic Java data from the input stream is added;
For example:
Package io; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; public class IOTest {public static void main (String [] args) throws IOException {// byte input stream DataOutputStream dos = new DataOutputStream (new FileOutputStream ("C: \ data. data "); DataInputStream dis = new DataInputStream (new FileInputStream (" C: \ data. data "); dos. writeDouble (1, 2.0235); dos. writeInt (1, 520); dos. flush (); System. out. println (dis. readDouble (); System. out. println (dis. readInt (); dos. close (); dis. close ();}}
PrintStream
PrintStream can describe other byte output streams and write formatted data to the byte output stream. The following example shows how to write formatted data to the console;
Package io; import java. io. IOException; import java. io. printStream; public class IOTest {public static void main (String [] args) throws IOException {// bytes output stream PrintStream ps = System. out; // output formatted data ps. println ("hello"); ps. printf ("% 5d \ n", 101); ps. write ('2'); ps. close ();}}
ObjectInputStream/ObjectOutputStream
Other byte output streams can be decorated, mainly used for Object serialization. An object can be written/read to a local file as a byte stream, as shown in the following example:
package io;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;class Dog implements Serializable { /** * */ private static final long serialVersionUID = -3581113588185842098L; private int age; public Dog(int age) { this.age = age; } public int getAge() { return this.age; }}public class IOTest { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("C:\\object.data")); ObjectInputStream input = new ObjectInputStream(new FileInputStream("C:\\object.data")); Dog dog = new Dog(2); output.writeObject(dog); output.flush(); Dog obj = (Dog) input.readObject(); System.out.println(obj.getAge()); output.close(); input.close(); }}
SequenceInputStream
Connect one or more inputstreams in a serial mode. Read the first stream and continue reading the second stream.
Package io; import java. io. byteArrayInputStream; import java. io. IOException; import java. io. inputStream; import java. io. sequenceInputStream; public class IOTest {public static void main (String [] args) throws IOException {// byte array in memory [] bArr = new byte [] {1, 2, 3}; byte [] bArr2 = new byte [] {4, 5, 6}; // byte input stream InputStream is = new ByteArrayInputStream (bArr ); // byte input stream InputStream is2 = new ByteArrayInputStream (bArr2); // byte input stream, connected to the above two streams SequenceInputStream sis = new SequenceInputStream (is, is2 ); byte [] bff = new byte [3]; // read the byte sis from the input stream. read (bff, 0, 3); System. out. println (bff [0] + "," + bff [1] + "," + bff [2]); // The data is read from the second byte stream. read (bff, 0, 3); System. out. println (bff [0] + "," + bff [1] + "," + bff [2]); is. close (); is2.close (); sis. close ();}}