Java core programming-byte stream of IO stream (2), javaio

Source: Internet
Author: User

Java core programming-byte stream of IO stream (2), javaio

1. byte stream

A 1.1-byte stream is a stream that operates byte. Byte streams can operate on any data, such as media data, music, movies, and images. Of course, they can also operate on characters. Byte stream is based on byte stream, we know that a byte is 8 binary bits, a byte corresponds to an English character in the UTF-8, people found that using a single byte to operate the text is more troublesome, therefore, we encapsulate byte streams. When we operate on text, we do not directly perform operations on byte streams, but directly perform operations on the encapsulated byte stream, that is, the hidden stream, this is more convenient.

1.2 we can see from the above summary that the use of byte stream operation characters is completely feasible. In this case, the character stream is used for character operations, but can the character stream perform operations on data other than characters?

1.3 if you operate data other than characters, such as multimedia data (music, movie, and image), for example, copying a music file, the program will not report an error during compilation, however, data may be lost during the operation. When you open the file after copying, the file may not be opened due to data loss. Therefore, we strongly recommend that you use byte streams for character text operations and for data other than characters.

Base classes of 1.4 byte streams: InputStream and OutputStream

2. byte stream class diagram

  2.1 InputStream class implementation structure

  

 2.2 OutputStream Structure Diagram

  

3. Use of common classes

  3.1 byte array as the input and output source (ByteArrayInputStream and ByteArrayOutputStream)

package se.io;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;public class ByteArrayStream {    public static void main(String[] args) {        try {            byte[] data = new byte[3];            data[0] = 100;            data[1] = 101;            data[2] = 102;            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);            byte[] buf = new byte[3];            byteArrayInputStream.read(buf);            System.out.println(new String(buf));            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();            byteArrayOutputStream.write(buf);            System.out.println(byteArrayOutputStream.toString());            byteArrayOutputStream.close();            byteArrayInputStream.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

  3.2 file as input and output source (FileInputStream and FileOutputStream)

package se.io;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FileStream  {    public static void main(String[] args) {        try {            FileInputStream fileInputStream = new FileInputStream("E:\\test\\file.txt");            byte[] buf = new byte[3];            fileInputStream.read(buf);            FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\file3.txt");            fileOutputStream.write(buf);            fileOutputStream.close();            fileInputStream.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

  3.3 Use an object as the input and output source (ObjectInputStream and ObjectOutputStream)

package se.io;import java.io.*;import java.util.Date;public class ObjectStream {    public static void main(String[] args) {        try {            FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\file.tmp");            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);            objectOutputStream.writeInt(100);            objectOutputStream.writeObject("hello,world");            objectOutputStream.writeObject(new Date());            FileInputStream fileInputStream = new FileInputStream("E:\\test\\file.tmp");            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);            System.out.println(objectInputStream.readInt());            System.out.println((String)objectInputStream.readObject());            System.out.println((Date)objectInputStream.readObject());        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }}

  3.4 As the input and output sources (PipedInputStream and PipedOutputStream)

Package se. io; import java. io. IOException; import java. io. pipedInputStream; import java. io. pipedOutputStream; public class PipedStream {public static void main (String [] args) {Sender sender = new Sender (); PipedOutputStream pipedOutputStream = sender. getPipedOutputStream (); Reciever reciever = new Reciever (); PipedInputStream pipedInputStream = reciever. getPipedInputStream (); try {pipedOutputStream. connect (pipedInputStream); new Thread (sender ). start (); new Thread (reciever ). start ();} catch (IOException e) {e. printStackTrace () ;}}// create a Sender class Sender extends Thread {private PipedOutputStream pipedOutputStream = new PipedOutputStream (); public PipedOutputStream extract () {return pipedOutputStream ;} public void run () {try {String s = new String ("hello, receive you"); pipedOutputStream. write (s. getBytes (); pipedOutputStream. close ();} catch (IOException e) {e. printStackTrace () ;}}// create recipient class Reciever extends Thread {private PipedInputStream pipedInputStream = new PipedInputStream (); byte [] buf = new byte [1024]; public PipedInputStream getPipedInputStream () {return pipedInputStream;} public void run () {String s = null; byte [] rec = new byte [1024]; try {int len = pipedInputStream. read (rec); s = new String (rec, 0, len); System. out. println (s); pipedInputStream. close ();} catch (IOException e) {e. printStackTrace ();}}}

  3.5 cache input stream output stream (BufferedInputStream and BufferedOutputStream)

package se.io;import java.io.*;public class BufferStream {    public static void main(String[] args) {        FileInputStream fileInputStream = null;        try {            fileInputStream = new FileInputStream("E:\\test\\file.txt");            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);            byte[] bytes = new byte[1024];            int i = bufferedInputStream.read(bytes);            FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\buffer.txt");            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);            bufferedOutputStream.write(bytes,0,i);            bufferedOutputStream.close();            bufferedInputStream.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

 3.6 data input and output streams (DataOutputStream and DataInputStream)

package se.io;import java.io.*;public class DataStream {    public static void main(String[] args) {        try {            FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\data.txt");            DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);            dataOutputStream.writeInt(123);            dataOutputStream.writeBoolean(true);            dataOutputStream.writeDouble(12.555);            FileInputStream fileInputStream = new FileInputStream("E:\\test\\data.txt");            DataInputStream dataInputStream = new DataInputStream(fileInputStream);            int i = dataInputStream.readInt();            boolean f = dataInputStream.readBoolean();            double d = dataInputStream.readDouble();            System.out.println(i);            System.out.println(f);            System.out.println(d);            dataInputStream.close();            fileInputStream.close();            dataOutputStream.close();            fileOutputStream.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }}

  

  

Related Article

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.