Java Bio Summary

Source: Internet
Author: User

, synchronous asynchronous, blocking non-blocking (not very clear at the moment, this blog after the completion of the post-processing)

1. Synchronous and asynchronous: Focus on the communication mechanism of the message,

Synchronization: After a call is initiated, the call is not returned if the result is not obtained, and the caller actively waits for the call to return.

Async: When a call is initiated, the call returns immediately, but not the result of the return, that is, the caller does not get the result immediately, but the caller notifies the caller, or the call is processed by a callback function.

2. Blocking and non-blocking: refers to the state that the program waits for when the result is called. Only synchronization can have blocking and non-blocking concepts.

Blocking: When a call is initiated, the thread is suspended until there is a return

Non-blocking: After a call is initiated, the current thread is not suspended, but continues execution.

Ii. Classification of IO streams

1. Input stream, output stream: Determines whether the stream is an input stream or an output stream, depending on the flow flow to the processor.

2. Byte stream, character stream: The data read by the byte stream will not be processed; the binary data read by the character stream will be converted to characters according to the corresponding encoding, character stream = byte stream + decode.

Third, IO flow class structure diagram

Iv. Common functions

Note: When you are finished using IO, be sure to close the stream

1.io stream to read a file

/*** Character Stream read/write text file * Remember to close the stream when using IO to read and write text files*/ Public classTest2 { Public Static voidMain (string[] args) {ReadFile (NewFile ("E:/b.txt")); WriteFile (NewFile ("E:/c.txt")); }         Public Static voidReadFile (file file) {FileReader fr=NULL; BufferedReader BR=NULL; InputStreamReader ISR=NULL; Try{System.out.println ("\r\n======== self-defined buffer array to read file ================"); Fr=Newfilereader (file); Char[] Buffer1 =New Char[3]; intLEN1 = 0;  while((Len1 = Fr.read (buffer1))! =-1) {System.out.print (NewString (buffer1,0, len1)); } System.out.println ("\r\n======== using buffered streams to read file ================"); BR=NewBufferedReader (Newfilereader (file)); String Temp= "";  while(temp = Br.readline ())! =NULL) {System.out.println (temp); } System.out.println ("\r\n======== using the conversion stream to read the file ==========="); ISR=NewInputStreamReader (NewFileInputStream (file), "Utf-8"); Char[] Buffer2 =New Char[1]; intLen2 = 0;  while((Len2 = Isr.read (buffer2))! =-1) {System.out.print (NewString (buffer2,0, Len2)); }                    } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally{            Try{br.close ();            Isr.close (); } Catch(IOException e) {e.printstacktrace (); }        }            }         Public Static voidWriteFile (file file) {FileOutputStream fos=NULL; FileWriter FW=NULL; BufferedWriter BW=NULL; Try {            //======== writing to a file using a byte stream ===========FOS =Newfileoutputstream (file); Fos.write ("Test writes to file abc123. ". GetBytes ()); Fos.write ("Test Overlay". GetBytes ());            Fos.flush (); //======== writing to a file using a character stream ===========FW =NewFileWriter (file,true); Fw.write ("\r\n======== writing to a file using a character stream ==========="); Fw.write ("This is data written with FileWriter");            Fw.flush (); //======== writes a file using a buffered stream ===========//The filewriter default is to overwrite the last data, so the append parameter is set to True when newBW =NewBufferedWriter (NewFileWriter (file,true));            Bw.newline (); Bw.write ("Write with buffered stream");        Bw.flush (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(NumberFormatException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally{            Try{bw.close ();                Fw.close ();            Fos.close (); } Catch(IOException e) {e.printstacktrace (); }        }    }}

2. Byte stream to copy a file

  

/*** Byte stream to copy a file **/ Public classTest1 { Public Static voidMain (string[] args)throwsioexception {copy (); }         Public Static voidcopy () {FileInputStream fis=NULL; FileOutputStream Fos=NULL; Try{FIS=NewFileInputStream ("E:/a.txt"); FOS=NewFileOutputStream ("E:/b.txt"); byte[] buffer =New byte[1024];//use arrays for buffering to increase IO speed            intLen = 0;  while(len = fis.read (buffer))! =-1) {fos.write (buffer,0, Len); }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally{            Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); }            Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); }                    }    }}

V. Some details of IO stream usage

In a 1.IO stream, the buffer class is the buffered* class, which does not have the ability to read and write, but only internally maintains a 8KB array that writes data to the hard disk only if close () and flush () are called or if the byte array is full. Improve the read and write efficiency of the file.

2. File output stream, if the destination file does not exist, the file will be created. Its write method empties the contents of the file by default, and then writes the content, which needs to be set in the construction method to succeed.

When the 3.FileOutputStream parameter is int, only the lower 8 bits are written out, and the remaining 24 bits are discarded.

4. The purpose of the conversion stream is to convert the byte stream to a character stream and to read and write the data according to the specified encoding.

Java Bio Summary

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.