Java basic review: file copy

Source: Internet
Author: User

(1) copying files using Byte input/output streams

Package day1_filecopy; 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 Demo1 {public static void main (String [] args) {File srcF = new File ("d:/Test/cmd.txt "); file destF = new File ("d:/Test/hi.txt"); fileCopy (srcF, destF); // whether it is text or image, can be copied successfully} public static void fileCopy (File s RcFile, File destFile) {// check if (srcFile = null | destFile = null) {System. out. println ("The file is null"); return;} InputStream is = null; OutputStream OS = null; try {is = new FileInputStream (srcFile ); OS = new FileOutputStream (destFile); // len indicates the number of bytes that have been read. If len! =-1 indicates that the file has been read. int len = 0; // buff buffer byte [] buff = new byte [1024]; while (len = is. read (buff ))! =-1) {OS. write (buff, 0, len);} System. out. println ("your file has been copied successfully! ");} Catch (IOException e) {e. printStackTrace ();} finally {try {OS. close (); // first close OS} catch (IOException e) {e. printStackTrace ();} finally {try {is. close (); // whether or not the OS is closed should be closed is} catch (IOException e) {e. printStackTrace ();}}}}}

In this way, exception handling is the most complicated part of the copy operation, especially when the resource is closed.

Since java 7, the "Automatic resource shutdown" feature has been added, which is called try-with-resource.

 

(2) use the new feature of automatically disabling resources to copy files

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 Demo2 {public static void main (String [] args) {File srcF = new File ("d:/Test/cmd.txt "); file destF = new File ("d:/Test/hi.txt"); fileCopy (srcF, destF); // whether it is text or image, all files can be copied successfully} public static void fileCopy (File srcFile, File destFile ){/ /Check if (srcFile = null | destFile = null) {System. out. println ("The file is null"); return;} try (InputStream is = new FileInputStream (srcFile); OutputStream OS = new FileOutputStream (destFile );) {// len indicates the number of bytes that have been read. If len! =-1 indicates that the file has been read. int len = 0; // buff buffer byte [] buff = new byte [1024]; while (len = is. read (buff ))! =-1) {OS. write (buff, 0, len);} System. out. println ("your file has been copied successfully! ");} Catch (IOException e) {e. printStackTrace ();}}}

As shown above, this method greatly reduces the code for exception handling and makes the program structure clearer.

The format is as follows:

Try (// declare the variable here and open the resource // The object must implement java. lang. autoCloseable Interface) {// here I/O operations} 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.