A simple summary of how stream flow is used in Java IO _java

Source: Internet
Author: User
Tags flush


The input and output functions of the Java language are very powerful and flexible, and the input and output operations of the data are carried out in the form of "stream". J2SDK provides a wide variety of "streaming" classes to get different kinds of data, defined in package java.io. In a program, input or output data through a standard method.
Streams in Java can be categorized from different perspectives:
According to the direction of the flow: divided into input and output streams.
Depending on the unit of processing data: Byte stream (8-bit) and character streams (16-bit).
Varies by function: divided into node flow and processing flow.
Node Flow: A stream (such as a file, memory) that can read and write data from a particular data source (node). It's like a single pipe that gets started on the tap.
Process flow: A "connection" over an existing stream (a node stream or a processing stream) that provides more powerful read and write functionality to the program by processing the data. It is as if a pipe (nodal flow) has been connected, and a few thicker, special functions of the pipe (the treatment flow) are set up to further handle the outflow of water.

All of the stream types provided by J2SDK in the package java.io inherit the following four types of abstract streams, respectively.

The four basic flow inputstream,outputstream,reader,writer have more specific subclasses, including file flow, buffer flow, data flow, conversion flow, print stream, object flow, etc., each have specific functions or to manipulate specific data. Where the deep color represents the node stream, the light color represents the processing stream.

This summarizes the specific usage of the stream through the node flow and the processing flow.
Classification of node streams:

The node stream is handled separately for the file (file), array (an array in memory), string (String), Pipe (pipe).
Classification of processing streams:

The

commonly used processing streams are: buffer flow, conversion flow, data flow, object flow, print flow.
Buffer Flow: to be nested above the corresponding byte stream, provides buffering for read and write data, improves the efficiency of reading and writing, and adds some more convenient methods. The function of the
buffer flow is like a bucket, the original word throttling is a water pipe, the water pipe directly connected to the destination, after adding a buffer flow, is a bucket under the water pipe, etc. after the bucket filled with water and then poured into the destination. Played a role in cushioning. This allows you to read more data at a time, avoiding frequent reads and writes to the hard drive. With buffering enabled, the Flush method is required to write data.

 import java.io.*; public class testbufferstream{public static void Main (string[] args) {try{bufferedwriter bw=new Writer (New FileWriter ("F:\\java\\io\\dat.txt"))//buffer flow on the byte stream of the write file BufferedReader br=new BufferedReader (New Filere 
       Ader ("F:\\java\\io\\dat.txt"));//A buffer stream string S=null is connected to the bytes of the read file;          for (int i=1;i<=100;i++) {s=string.valueof (Math.random ());//Assign value to s by random function bw.write (s);          Writes s to the Dat.txt file Bw.newline (); Writes a line break. 
       A buffer stream is a good way to write or read a row of data.            } bw.flush (); 
       Causes all data in memory to be immediately written out and no longer buffered. 
       while ((S=br.readline ())!=null) {//Read the contents of the file by rows System.out.println (s);            } bw.close (); 
       Closes the processing stream and closes the inner node stream. 
       
    Br.close (); 
    catch (IOException e) {e.printstacktrace (); } 
  } 
} 

Convert stream: converts from byte data to character data. InputStreamReader needs and InputStream sockets, OutputStreamWriter need and OutputStream sockets.
Data Flow: DataInputStream and dataoutputstream  provide a way to write the underlying data type to a file, or read it, the stream is of great use, and if there is no such flow, Has a long, itself only 8 bytes, if I want to write to a file, need to turn into a string, and then in a character array, that space will occupy a lot, but with this flow is very convenient, directly to write these 8 bytes to the file can be, not only save the memory space also makes the program easier to write. But when reading, you need to be aware that, depending on the type of data being read, the pointer moves down, so the order you read must be consistent with the written order to fulfill your correct needs. Otherwise, it would be quite similar to splitting the data.

 import java.io.*; public class testdatastream{public static void Main (string[] args) {Bytearrayoutputstream baos=new bytearrayout  Putstream (); 
     Build a node stream.     DataOutputStream dos=new DataOutputStream (BAOs);            A stream of data flows "pipe" try{dos.writedouble (Math.random ()) on the node stream;                Double, accounting for 8 bytes Dos.writeboolean (true);  
       Boolean type, accounting for one byte bytearrayinputstream bais=new bytearrayinputstream (Baos.tobytearray ());         System.out.println (Bais.available ());    Total number of bytes available in the output stream--9 datainputstream dis=new datainputstream (Bais);         The data stream "pipe" System.out.println (dis.readdouble ()) is also connected outside the output stream.        The number System.out.println (Dis.readboolean ()) of the double type is read directly; 
       Direct readout of the Boolean dos.close (); 
     Dis.close (); 
     }catch (IOException e) {e.printstacktrace (); } 
 
   } 
} 


Print Flow:java.io specifically provides the flow for printing, which has the function of automatic flush when writing, so it is not necessary to print one thing every flush once.

Import java.io.*; 
public class testprintstream1{public 
    static void Main (string[] args) { 
      printstream ps=null; 
      try{ 
       fileoutputstream fos=new fileoutputstream ("F:\\java\\io\\log.dat");//Create output stream, specify output location 
       ps=new PrintStream (FOS);                      Wrap the print stream outside the stream 
      }catch (IOException e) { 
       e.printstacktrace (); 
      } 
      if (ps!=null) { 
       system.setout (PS);                         Set the system printing to print stream PS 
      } 
      int ln=0; 
      for (char c=0;c<=60000;c++) { 
        System.out.print (c+ "");                     does not print to a DOS window, the output stream is printed directly to the specified file 
        if (ln++>=100) { 
           System.out.println (); ln=0 
        } 
}}} 

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.