Solved

copying a folder and its contents(subfolders and files) from one location to another

Posted on 2003-10-29
5
379 Views
Last Modified: 2010-03-31
Hi Experts,

I'm trying to copy a folder containing several files and subfolders which also contain files and subfolders, etc... I don't know the names of the subfolders nor the files.

I would like to copy this main folder (c:\myfolder) to another location (c:\test\myfolder) with all its content. How do I achieve this. i havn't as yet found a way of copying a folder and its contents in java.



Thank you for your time,

codechewer
0
Comment
Question by:codechewer
[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
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9642982
Might be easiest to Runtime.exec("xcopy...
0
 

Author Comment

by:codechewer
ID: 9643372
thanks CEHJ. that was really helpfull but my environment takes the api as Runtime.getRuntime().exec("xcopy...

i have tried it with dir, mkdir which runs perfectly on the commandline but throws and exception as

CreateProcess: mkdir myfoldername  error:  2

where mkdir myfoldername is a commandline command.

usign jbuilder 6 and tomcat 4.1.24

be most grateful to here from you.
0
 
LVL 4

Expert Comment

by:brunomsilva
ID: 9644350
why don't you try this:

public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
    int i;
    File sourceFile;
    File destinationFile;
    File[] files = aSourceDirectory.listFiles();

    if (!aDestinationDirectory.exists())
      aDestinationDirectory.mkdirs();

    for (i=0; i<files.length; i++) {
      sourceFile = files[i];
      destinationFile=new File(aDestinationDirectory, sourceFile.getName());
      if (sourceFile.isDirectory()) {
        if (!destinationFile.exists())
          destinationFile.mkdir();
        copyDirectory(sourceFile, destinationFile);
      }
      else {
        copyFile(sourceFile, destinationFile);
      }
   }
}

cheers,
  bruno
0
 
LVL 4

Accepted Solution

by:
brunomsilva earned 250 total points
ID: 9644364
i'm sorry i forgot the copyFile method. here it is

public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
    FileInputStream inputStream;
    FileOutputStream outputStream;
    int nrBytesRead;
    byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];

    inputStream = new FileInputStream(aSourceFile);
    try {
      File directory = new File(aDestinationFile.getParent());
        if (directory!=null && !directory.exists()){
          directory.mkdirs();
      }
      outputStream = new FileOutputStream(aDestinationFile);
      try {
        do {
          nrBytesRead = inputStream.read(buffer);
          if (nrBytesRead>0)
            outputStream.write(buffer, 0, nrBytesRead);
        }
        while (nrBytesRead>=0);
      }
      finally {
        outputStream.close();
      }
    }
    finally {
      inputStream.close();
    }
  }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9644405
Sorry - those command are shell commands so it would have to be:

Runtime.getRuntime().exec("cmd.exe dir ...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

696 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