Solved

How to do a quick file copy

Posted on 2002-03-05
3
203 Views
Last Modified: 2010-08-05
I have a file c:\directory1\file1.mdb
that I want to copy to c:\directory2 and overwrite file1.mdb in directory2 if it exists in there.
How is this done in java?
0
Comment
Question by:tom_corc
3 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6842303
0
 
LVL 92

Expert Comment

by:objects
ID: 6842387
Write a batch file and execute it from your Java app :-)
Java does not have a specific method for copying a file, the only way to do is to read the existing file, and write the new one.
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 6843715
It is easy to do it yourself:
public static void copyFile(File f1, File f2) throws IOException {
    FileInputStream in = new FileInputStream(f1);
    FileOutputStream out = new FileOutputStream(f2);
    byte[] buf = new byte[2048];
    int r;
    while ((r=in.read(buf)) != -1) {
        out.write(buf, 0, r);
    }
}
public static void copy(File f1, File f2) throws IOException {
    if (f1.isDirectory()) {
        f2.mkdirs();
        String[] files = f1.list();
        for (int i =0; i < files.length; i++) {
            copy(new File(f1, files[i]), new File(f2, files[i]));
        }
    } else {
        copyFile(f1, f2);
    }
}
public static void copy(String f1, String f2) throws IOException {
    copy(new File(f1), new File(f2));
}


Use it this way:
try {
  copy("c:/ivv", "c:/ivv_copy");
} catch (IOException ioe) {
  ioe.printStackTrace();
}
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 72
MySQL  on Tomcat 8 48
runtime exception 2 30
maven module vs maven project 3 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question