Solved

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

Posted on 2003-10-29
5
375 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

831 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