(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 ();}