Java IO Stream Analytics Collation

Source: Internet
Author: User

classification of Java IO streams streams in Java can be categorized from different angles. Sort by flow: input stream: The stream from which the program can read data.
Output stream: The stream to which the program can write data. By Data Transmission Unit classification:

BYTE stream: Processed in bytes (8-bit binary). Binary data primarily used to read and write images or sounds.

Character stream: Processed in characters (16-bit binary).

are implemented by means of a byte stream. Character stream is the byte stream is encapsulated, convenient operation. At the bottom, all input and output are in bytes.

The suffix is stream is a byte stream, and the prefix is Reader,writer is a character stream.

Classification by Function:

Node stream: A stream class that reads and writes from a specific place, such as a disk or a piece of memory area.

Filter stream: Use node stream as input or output. The filter stream is created using an existing input stream or an output stream connection.

different input and output streams

The stream provided by the JDK inherits four classes:InputStream (Byte input stream), OutputStream (byte output stream), Reader (character input stream), Writer (character output stream).

BYTE input stream:

Byte input stream abstract class and its key methods: Class InputStream
java.io.InputStreamabstract int Read ()
Reads the next byte of data from the input stream.
int read (byte[] b)
Reads a certain number of bytes from the input stream and stores it in buffer array B.
int read (byte[] b, int off, int len)
Reads up to Len data bytes in the input stream into a byte array. Operation of the input stream: open an input stream
Loop Read
close the input stream

Here you use the InputStream subclass FileInputStream to read the file:

public static void Main (string[] args) throws IOException {    //create file input stream    InputStream is = new FileInputStream ("d:/it Zhai/arthinking.txt ");    Create byte buffer    byte[] buffer = new BYTE[100];    int length = 0;    Iterates through the file in bytes    while (length = is.read (buffer, 0, buffer.length))! =-1) {        //converts bytes to characters and outputs        String str =new String (buffer, 0, length);        System.out.println (str);}    }
Class hierarchy of abstract class InputStream:

byte array input stream Bytearrayinputstream

Takes a byte array as the input stream of the source.

Related examples:

byte array input stream:

public static void Main (string[] args) {    //create read data source    String input = "arthinking";    Gets the byte array    byte[] b = input.getbytes ();    Create byte array output stream    Bytearrayinputstream bis = new Bytearrayinputstream (b);    Loop-by    -read for (int i = 0; i < input.length (); i++) {        int c;        Read the next byte        while ((c = Bis.read ())! =-1) {            System.out.print ((char) c);        }    }    Resets the position of the buffer to the marker position    bis.reset ();}

 

byte array output stream:

public static void Main (string[] args) throws IOException {    //create byte output stream    bytearrayoutputstream bos = new ByteArray OutputStream ();    String output = "arthinking";    Create a byte array to output    byte[] buffer = output.getbytes ();    Writes a byte array to the output stream    bos.write (buffer);    Create a file output stream    outputstream os = new FileOutputStream ("D:/itzhai/arthinking.txt");    Writes the byte output stream to the file output stream    bos.writeto (OS);}

  

BYTE output stream:

The abstract class of the byte output stream and its most critical method:
Class OutputStreamjava.lang.Objectjava.io.OutputStreamvoid write (byte[] b)
Writes a b.length byte from the specified byte array to this output stream.
void Write (byte[] b, int off, int len)

 

Writes the Len byte in the specified byte array from offset off to this output stream.
abstract void Write (int b)
Writes the specified bytes to this output stream.

It can be seen that only the last method is abstract, because the first two calls the third abstract method, so that the subclass of the abstract class must provide an abstract write (int b) implementation, so that each subclass implementation is different.

Operation of the output stream: Open the output stream
Looping writes
close the input stream

This uses the OutputStream subclass FileOutputStream output to the file:

public static void Main (string[] args) throws IOException {    //create an output stream    outputstream os = new FileOutputStream ("d:/ Itzhai/arthinking.txt ", true);    String output = "http://www.itzhai.com";    Gets the byte array from the string    byte[] buffer = output.getbytes ();    Writes out to the output stream    os.write (buffer);    Close output stream    os.close ();}

  

Class hierarchies for abstract class OutputStream:

Filter Flow:

The filter stream does not work directly with the file, but only through the node stream. Can be seen from its construction method:

Filteroutputstream (OutputStream out)

Need to pass in a outputstream.

In the subclasses of InputStream and OutputStream,

FilterInputStream and Filteroutputstream are filter streams, which derive subclass DataInputStream and dataoutputstream data input streams and data output streams.

The main feature of the filter stream is the conversion of the specified type or format to the transmitted data at the same time as the input and output data.

buffered output stream Bufferedoutputstream

This class implements a buffered output stream. By setting this output stream, the application can write individual bytes to the underlying output stream without having to call the underlying system for each byte write.

When the buffer fills or closes the output stream, it is output to the stream at once, or the flush () method is called to actively output the buffer to the stream.

Examples of use of filter streams:

Examples of bufferedoutputstream and DataOutputStream decorative filteroutputstream using a filter flow class :

public static void Main (string[] args) throws IOException {    //create data output stream    dataoutputstream dos = new DataOutputStream (            new Bufferedoutputstream (New FileOutputStream ("D:/itzhai/arthinking.txt"));    byte a = 1;    Char b = ' a ';    int c = 2;    Use the data output stream object method to write out the data to the output stream    dos.write (a);    Dos.write (b);    Dos.write (c);    Close the data output stream    dos.close ();    Create data input stream    datainputstream dis = new DataInputStream (New            bufferedinputstream ("d:/ Itzhai/arthinking.txt ")));    Use the data input stream method to read data from the input stream    System.out.println (dis.readbyte () + Dis.readchar () + dis.readint ());    Close data input stream    dis.close ();}

  

General steps for using DataInputStream and DataOutputStream data file streams:

    • Create a byte file stream object
    • Creating a data file stream object based on a byte file stream object
    • Input/output of basic types of data by means of data file stream objects
Character input stream:

Character output stream:

Java IO Stream Analytics Collation

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.