Java simple operations on file copying

Source: Internet
Author: User

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

 

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.