Solved

How to do a quick file copy

Posted on 2002-03-05
3
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 53
swing controls 2 23
hashmap order 17 42
Java array 21 86
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

733 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