Process Stream _ Buffer stream

Source: Internet
Author: User

1 can improve the efficiency of file operation compared with node stream

2 Flush operation outputs the remaining string in the last cache

 Packagelianxi1;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;Importorg.junit.Test; Public classtestbuffered {@Test Public voidtestcopy () {LongStart =System.currenttimemillis (); String src= "D:/io/plan.png"; String des= "Prison.png";    CopyFile (Src,des); LongEnd =System.currenttimemillis (); System.out.println (End-start);}  Public voidCopyFile (String src,string des) {FileInputStream fis=NULL; FileOutputStream Fos=NULL; Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; File file1=NewFile (SRC); File file2=NewFile (DES); Try{FIS=NewFileInputStream (FILE1); FOS=NewFileOutputStream (file2); Bis=NewBufferedinputstream (FIS); Bos=NewBufferedoutputstream (FOS); intLen; byte[] B =New byte[100];  while((Len=bis.read (b))!=-1) {Bos.write (b,0, Len);        Bos.flush (); }    }Catch(Exception ex) {ex.printstacktrace ();} finally{        if(bis!=NULL){            Try{bis.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        if(bos!=NULL){            Try{bos.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }} @Test Public voidtest2 () {FileReader fr=NULL; FileWriter FW=NULL; BufferedReader BR=NULL; BufferedWriter BW=NULL; File file1=NewFile ("D:/io/hellotext.txt"); File file2=NewFile ("Tangshan"); Try{FR=NewFileReader (FILE1); FW=NewFileWriter (file2); BR=NewBufferedReader (FR); Bw=NewBufferedWriter (FW);//int len;//char[] C = new char[100];//While ((Len=br.read (c))!=-1) {//Bw.write (c, 0, Len);//Bw.flush ();//        }        //or using the ReadLine methodString str;  while((Str=br.readline ())! =NULL) {bw.write (str);        Bw.newline (); }    }Catch(Exception ex) {ex.printstacktrace ();} finally{        if(br!=NULL){            Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        if(bw!=NULL){            Try{bw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }}}

Process Stream _ Buffer stream

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.