Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jboss wildfly 10.1 10 232
jboss 7.1 start up error 1 39
Fast way to search item into Java Array (Rhino compatible) 2 31
web project error add remove 1 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

829 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