Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now