IO: Buffering

Source: Internet
Author: User

Explanation of principle: http://ifeve.com/java-io-char-buffered-filter/

Bufferedinputstream byte stream Input buffer:

1 voidBufferedinputstream () {2         //Bufferedinputstream with buffers, multithreading visible3         //Reading Chinese will be garbled4         //no ReadLine ()5File File =NewFile ("E:\\abcdata.txt");6InputStream InputStream =NULL;7Bufferedinputstream bis =NULL;8         byte[] B =New byte[1024];9         intLen;TenStringBuilder SB =NewStringBuilder (); One         Try { AInputStream =Newfileinputstream (file); -bis =NewBufferedinputstream (inputstream); -              while(len = Bis.read (b))! =-1) { theString string =NewString (b, 0, Len, "UTF-8")); - sb.append (string); -             } - System.out.println (SB); +}Catch(FileNotFoundException e) { -             //TODO auto-generated Catch block + e.printstacktrace (); A}Catch(IOException e) { at             //TODO auto-generated Catch block - e.printstacktrace (); -}finally { -             if(bis! =NULL) { -                 Try { - bis.close (); in}Catch(IOException e) { -                     //TODO auto-generated Catch block to e.printstacktrace (); +                 } -             } the         } *  $}

Bufferedoutputstream byte throttle Output buffer:

1 voidBufferedoutputstream () {2File File =NewFile ("E:\\abcdata.txt");3OutputStream OutputStream =NULL;4Bufferedoutputstream BOS =NULL;5         byte[] B =New byte[1024];6String s = "Test output!! ";7         Try {8OutputStream =NewFileOutputStream (file,true);9BOS =NewBufferedoutputstream (outputstream);Tenb =s.getbytes (); One Bos.write (b); A             //Refresh Buffering - Bos.flush (); -}Catch(FileNotFoundException e) { the             //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { -             //TODO auto-generated Catch block + e.printstacktrace (); -}finally { +             if(Bos! =NULL) { A                 Try { at bos.close (); -}Catch(IOException e) { -                     //TODO auto-generated Catch block - e.printstacktrace (); -                 } -             } in         } -}

BufferedReader character stream Input buffer:

1 voidBufferedReader () {2File File =NewFile ("E:\\test.txt");3BufferedReader br =NULL;4Reader reader =NULL;5 string string;6StringBuilder SB =NewStringBuilder ();7         Try {8Reader =Newfilereader (file);9BR =NewBufferedReader (reader);Ten              while(String = Br.readline ())! =NULL) { One sb.append (string); A             } - System.out.println (SB); -}Catch(FileNotFoundException e) { the             //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { -             //TODO auto-generated Catch block + e.printstacktrace (); -}finally { +             if(BR! =NULL) { A                 Try { at br.close (); -}Catch(IOException e) { -                     //TODO auto-generated Catch block - e.printstacktrace (); -                 } -             } in         } -  to}

BufferedWriter character stream output buffering:

1 voidBufferedWriter () {2File File =NewFile ("E:\\test.txt");3Writer writer =NULL;4BufferedWriter BW =NULL;5String string = "Test Tests!! ";6         Try {7writer =NewFileWriter (file,true);8BW =NewBufferedWriter (writer);9 bw.write (string);Ten Bw.flush (); One}Catch(IOException e) { A             //TODO auto-generated Catch block - e.printstacktrace (); -}finally { the             if(BW! =NULL) { -                 Try { - bw.close (); -}Catch(IOException e) { +                     //TODO auto-generated Catch block - e.printstacktrace (); +                 } A             } at         } -  -}

IO: Buffering

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.