Package fileInputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import javax. swing. JOptionPane; public class Test3 {/*** @ param args * Implementation function: copy a file to a specified path */public static void copyFile (String url1, String url2) {// get the specified File to be copied File file1 = New File (url1); InputStream is = null; // obtain the specified copy path File file2 = new File (url2); OutputStream OS = null; file file3 = new File (file2, "2.txt"); // You can also determine whether the input path is valid. if (! File2.exists () {System. out. println ("The path does not exist, whether to create it"); // The user int var = JOptionPane is prompted. showConfirmDialog (null, "the specified file path does not exist. Do you want to create it ?? "); If (var = 0) {file2.mkdirs (); // if the specified path does not exist, create a try {// object for creating the file input stream is = new FileInputStream (file1); // create the object OS = new FileOutputStream (file3) of the file output stream ); // create a buffer byte [] buffer = new byte [1024]; // create a variable int len = 0 to determine the actual read length; // read resource data into the buffer while (len = is. read (buffer ))! =-1) {// outputs resource data OS from the buffer zone. write (buffer, 0, len); // flush () is to forcibly output the data in the buffer to the OS. flush () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (is! = Null) {try {// close stream resource is. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} if (OS! = Null) {try {// disable stream resources? OS. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}} else {JOptionPane. showMessageDialog (null, "Please reselect the path! ") ;}} Else {try {// create object of the file input stream is = new FileInputStream (file1 ); // create the object OS = new FileOutputStream (file3) of the file output stream; // create a buffer byte [] buffer = new byte [1024]; // create the variable int len = 0 to determine the actual read length; // read the resource data into the buffer while (len = is. read (buffer ))! =-1) {// outputs resource data OS from the buffer zone. write (buffer, 0, len); // flush () is to forcibly output the data in the buffer to the OS. flush () ;}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (is! = Null) {try {// close stream resource is. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} if (OS! = Null) {try {// disable stream resources? OS. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}}} public static void main (String [] args) {// TODO Auto-generated method stub // defines the object to be copied String url1 = "F: \ test \ 1.txt "; // define the specified Copy location, that is, the file name String url2 = "F: \ test \ aa \ bb \ c \ d"; // call the method of copying a file, copyFile (url1, url2 );}}