Java IO Summary (i)

Source: Internet
Author: User

The IO stream in Java is the basis for implementing input and output, where the flow is the data stream, and most of the flow-related classes are placed under the Java.io package.

One, the stream classification of Java

According to the different classification of the flow can be divided into different types, below we from different angles of convection classification.

1, according to the direction of the flow can be divided into the input stream and output stream

Input stream: Data can only be read from, not written to

output stream: You can only write data to it and not read it from it. 2, byte stream and character stream

The byte stream mainly takes InputStream and OutputStream as the base class, its corresponding subclass has FileInputStream and fileoutputstream realization file reads and writes, The Bufferedinputstream and bufferedoutputstream provide the buffer function, and the minimum unit data for the operation is 8-bit bytes. The character stream is mainly based on reader and writer, its corresponding subclass FileWriter and FileReader can realize the file read and write operation,BufferedWriter and The BufferedReader is capable of providing a buffer function to increase efficiency, and the smallest unit data for operation is 16-bit characters.

3, node flow and processing flow

Node Flow: You can read/write data flows from/to a specific IO device. The program is connected directly to the data source (physical node).

Process flow: Wrapping a node stream, he can hide the differences between the underlying devices, providing a better input/output approach.

Character Stream

Example 1: Writing a stream of characters
 Public classFileinputstreamtest { Public Static voidMain (string[] args)throwsIOException {FileWriter fw=NULL; Try{FW=NewFileWriter ("D:\\javapro\\iojava\\xin.txt"); Fw.write ("What you say, I don't listen \ r \ n"); Fw.write ("Let the story extend \ r \ n"); }  Catch(IOException e) {e.printstacktrace (); }  finally        {            if(fw!=NULL) {fw.close (); }        }    }}
Example 2: Reading of a character stream
 Public classReadstreamtest { Public Static voidMain (string[] args)throwsIOException {FileReader fr=NULL; Try{FR=NewFileReader ("D:\\javapro\\iojava\\ha.txt"); Char[] ch =New Char[1024]; intnum;  while(num = fr.read (ch))! =-1) {System.out.println (NewString (ch,0, num)); }        }   Catch(IOException e) {e.printstacktrace (); }  finally {            if(fr!=NULL) {fr.close (); }        }    }}
Example 3: Writing a byte stream
 Public classOutputtest { Public Static voidMain (string[] args)throwsIOException {fileinputstream fi=NULL; FileOutputStream fo=NULL; Try{fi=NewFileInputStream ("D:\\javapro\\iojava\\ha.txt"); Fo=NewFileOutputStream ("D:\\javapro\\iojava\\xin.txt"); byte[] by =New byte[1024]; intnum;  while(num = Fi.read (by))!=-1) {Fo.write (by,0, num); }        }  Catch(IOException e) {e.printstacktrace (); } finally        {            if(fi!=NULL) {fi.close (); }            if(fo!=NULL) {fo.close (); }        }    }}
Example 4: Reading of a byte stream
 Public classTest { Public Static voidMain (string[] args)throwsIOException {String str= "D:\\javapro\\iojava\\ha.txt";        FileInputStream F; F=NewFileInputStream (str); byte[] Temp1 =New byte[1024]; intnum;  while(num = F.read (TEMP1)) >0) {System.out.println (num);            System.out.println (TEMP1); System.out.println (NewString (temp1,0, num));    } f.close ();; }}

All of these are examples of node streams, and here's an example of a process flow

Example 5: Process flow
 Public classFileinputstreamtest { Public Static voidMain (string[] args)throwsIOException {printstream ps=NULL; Try        {            //Create a node streamFileOutputStream fs =NewFileOutputStream ("D:\\javapro\\iojava\\xin.txt"); //wrapping FileOutputStream output stream with PrintStreamPS =NewPrintStream (FS); //using PrintStream to perform outputPs.println ("normal character"); //directly using the PrintStream output objectPs.println (Newfileinputstreamtest ()); }  Catch(IOException e) {e.printstacktrace (); }  finally        {            if(ps!=NULL) {ps.close (); }        }    }}

It can be seen from the above that the process flow is very simple, only need to pass a node stream as a constructor parameter when creating the process flow, so that the process flow created is the process flow containing the node stream ...

Java IO Summary (i)

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.