Java implements local FileCopy

Source: Internet
Author: User

Objective:

Java streaming is an important content, the basic file read and write and copy knowledge point is a lot of interviews. Therefore, this paper makes a simple test summary.

2. Figure shows "text io/binary io"(this is a summary of the reference from the Web, quite classic, easy to compare memory)

3. Text copy of the experimental Java Implementation code:
1  PackageCom.gdufe.io;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.DataInputStream;6 ImportJava.io.DataOutputStream;7 ImportJava.io.FileInputStream;8 ImportJava.io.FileOutputStream;9 Importjava.io.IOException;Ten ImportJava.io.InputStream; One ImportJava.io.OutputStream; A  -  Public classTestinputoutputstream { -  the //private final static String source= "T.txt"; - //private final static String target= "Tt.txt"; - //private final static String source= "P.png"; - //private final static String target= "Pp.png"; +     Private Final StaticString source= "D:\\tool_software\\mysql-installer-community-5.6.23.0.msi"; -     Private Final StaticString target= "Mysql-installer-community-5.6.23.0.msi"; +     //D:\Tool_Software\Ryuyan.zip A      at      -      Public Static voidMain (string[] args) { - //testinputoutputstream.copy1 (SOURCE, TARGET); - //testinputoutputstream.copy2 (SOURCE, TARGET); - testinputoutputstream.copy3 (SOURCE, TARGET); -System.out.println ("--end---"); in     } -      to      Public Static voidcopy1 (String src,string tar) { +          -InputStream input =NULL; theOutputStream output =NULL; *         Try{ $input =NewFileInputStream (SRC);Panax NotoginsengOutput =NewFileOutputStream (TAR); -             intR; the              while((R=input.read ())!=-1){ +Output.write ((byte) r); A             } the}Catch(Exception e) { + e.printstacktrace (); -}finally{ $             Try { $ input.close (); - output.close (); -}Catch(IOException e) { the e.printstacktrace (); -             }Wuyi         } the          -     } Wu      Public Static voidcopy2 (String src,string tar) { -InputStream input =NULL; AboutOutputStream output =NULL; $         Try{ -input =NewFileInputStream (SRC); -Output =NewFileOutputStream (TAR); -             intLength=0; A             byte[]B =New byte[1024]; +              while((Length=input.read (b))!=-1){ theOutput.write (b,0, length); -             } $}Catch(Exception e) { the e.printstacktrace (); the}finally{ the             Try { the input.close (); - output.close (); in}Catch(IOException e) { the e.printstacktrace (); the             } About         } the     } the      the      Public Static voidcopy3 (String src,string tar) { +InputStream input =NULL; -OutputStream output =NULL; the         Try{Bayiinput =NewDataInputStream (NewBufferedinputstream (NewFileInputStream (SRC))); theOutput =NewDataOutputStream (NewBufferedoutputstream (NewFileOutputStream (TAR))); the             /*** After the pro-test, buffer buffered stream reads really faster some * * **/ -              -             intLength=0; the             byte[]B =New byte[1024];//first read the stream into a byte array the              while((Length=input.read (b))!=-1){ theOutput.write (b,0, length); the             } -}Catch(Exception e) { the e.printstacktrace (); the}finally{ the             Try {94 input.close (); the output.close (); the}Catch(IOException e) { the e.printstacktrace ();98             } About         } -     }101}

(Attached: The reference code when the attention to prepare the test file, otherwise the exception "file can ' t be found"!)

Copy files to the direct directory of the Java Project, refresh project to view!

Java implements local FileCopy

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.