How to copy the source file content to the target file in Java

Source: Internet
Author: User
Package io_test; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; public class iotest {public static void main (string [] ARGs) {file filesrcfile1 = new file ("E: \ new file folder .rar "); file filedestfile1 = new file ("E: \ demo \ new Folder. rar "); file filesrcfilef1 = new file (" e :\\ new folder. rar "); file filedestfilef1 = new file (" e :\demo \ new folder. rar "); try {long start1 = system. currenttimemillis (); copyfile1 (filesrcfile1, filedestfile1); long end1 = system. currenttimemillis (); system. out. println ("copyfile1 time consumed:" + (end1-start1)/1000.0); long start2 = system. currenttimemillis (); copyfile2 (filesrcfilef1, filedestfilef1); long end2 = SY Stem. currenttimemillis (); system. out. println ("copyfile2 time used:" + (end2-start2)/1000.0);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}} /*** copy the content of the source file to the target file ** @ Param SRC * Source File * @ Param DEST * target file */public static void copyfile1 (File SRC, file DEST) throws ioexception {bufferedinputstream Bis = NULL; bufferedoutputstream Bos = NULL; byte [] B = new byte [8192]; try {bis = New bufferedinputstream (New fileinputstream (SRC); Bos = new bufferedoutputstream (New fileoutputstream (DEST); For (int count =-1; (COUNT = bis. read (B ))! =-1;) {Bos. write (B, 0, count);} Bos. flush () ;}catch (ioexception e) {Throw e ;}finally {close (bis); close (BOS );}} /*** copy the content of the source file to the target file ** @ Param srcfile * Source File * @ Param destfile * target file */public static void copyfile2 (File srcfile, file destfile) throws ioexception {fileinputstream input = new fileinputstream (srcfile); try {fileoutputstream output = new fileoutputstream (destfile); try {byte [] buffer = new Byte [4096]; int n = 0; while (-1! = (N = input. Read (buffer) {output. Write (buffer, 0, n) ;}} finally {try {If (output! = NULL) {output. Close () ;}catch (ioexception IOE) {// ignore }}} finally {try {If (input! = NULL) {input. close () ;}} catch (ioexception IOE) {// ignore }}/ *** close both the input stream and output stream, convert the thrown exception to runtimeexception ** @ Param is * @ Param OS */public static void close (inputstream is, outputstream OS) {close (is ); close (OS);}/*** close the tool and method of the input stream, and convert the exception that may be thrown to runtimeexception ** @ Param is */public static void close (inputstream is) {If (is! = NULL) {try {is. close ();} catch (ioexception e) {e. printstacktrace () ;}}/ *** disable the tool and method of the output stream, convert the thrown exception to runtimeexception ** @ Param OS */public static void close (outputstream OS) {If (OS! = NULL) {try {OS. Close ();} catch (ioexception e) {e. printstacktrace ();}}}}

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.