Java Foundation-io Stream

Source: Internet
Author: User
Tags file copy

The simple use of Io flow is very simple ~

 PackageCom.j2se.d59.demo;ImportJava.io.*;//the objects in IO are from java.io.*;//The input and output of the IO stream is relative to the memory Ram//Categories: units "byte stream, character stream" or direction "input stream, output stream" or function "node flow, processing Flow"//All IO streams are extended by four abstract classes://subclasses of InputStream FileInputStream Bufferedinputstream//subclasses of OutputStream FileInputStream Bufferedoutputstream//subclass of Reader FileReader BufferedReader//Writer's subclass FileWriter BufferedWriter Public classIotest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//The following code uses the byte stream and the character stream 2 ways to complete the file copy function//1 PreparationString path1 = "d:\\java\\test\\ Red Mansions-Cao Xueqin"; String path2= "d:\\java\\test\\ program creation: Dream of Red Mansions-Cao Xueqin"; String Path3= "d:\\java\\test\\ Red Mansions-Cao Xueqin-copy. txt"; String Path4= "d:\\java\\test\\ program creation: Red Mansions-Cao Xueqin-copy. txt"; byte[] B =New byte[1024 * 256]; Char[] C =New Char[1024 * 256]; //2 StatementFileInputStream FIS =NULL; Bufferedinputstream bis=NULL; FileOutputStream Fos=NULL; Bufferedoutputstream Bos=NULL; FileReader FR=NULL; BufferedReader BR=NULL; FileWriter FW=NULL; BufferedWriter BW=NULL; //3 Create        Try{FIS=NewFileInputStream (path1); Bis=NewBufferedinputstream (FIS); FOS=NewFileOutputStream (path2); Bos=NewBufferedoutputstream (FOS); Fr=NewFileReader (PATH3); BR=NewBufferedReader (FR); FW=NewFileWriter (PATH4); Bw=NewBufferedWriter (FW); //4 Read            intIB =Bis.read (b); //5 Judgment-Method 1            LongT1 =System.currenttimemillis ();  while(Ib > 0) {                //6 ReplicationBos.write (b, 0, IB); //7 CyclesIB =Bis.read (b); }            //8 RefreshBw.flush (); Longt2 =System.currenttimemillis (); System.out.println ("When copying files using the byte stream is complete, time:" + (T2-T1) + "milliseconds"); LongT3 =System.currenttimemillis (); intIC =Br.read (c); //Judgment-Method 2             while(IC > 0) {                //CopyBw.write (c, 0, IC); //LoopsIC =Br.read (c); }            //RefreshBw.flush (); LongT4 =System.currenttimemillis (); System.out.println ("Use character stream to copy file completion, spents:" + (T4-T3) + "milliseconds"); //============= The following are all exception handling code snippets, you can not look at =============}Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            //9 Close            if(bis! =NULL) {                Try{bis.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(FIS! =NULL) {                Try{fis.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 (); }            }            if(FIS! =NULL) {                Try{fis.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(FR! =NULL) {                Try{fr.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 (); }            }            if(FW! =NULL) {                Try{fw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }}

Java Foundation-io Stream

Related Article

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.