2

I want to read a directory and create the same file and sub directories in another directory.

The path for the directory will be a user defined value.

private static void readFiles(File sourceFolder, String destePath) {
        if (sourceFolder.isDirectory())
            for (File sourceFile : sourceFolder.listFiles()) {
                if (sourceFile.isFile()) {
                    File destFile = new File(destePath + "/"
                            + sourceFile.getName());
                    updateConnectorXML(sourceFile, destFile);
                } else {
                    {
                        destePath = destePath + "/" + sourceFile.getName();
                        File destFile = new File(destePath);
                        destFile.mkdir();
                        readFiles(sourceFile, destePath);
                    }
                }

            }
    }

here e.d sourceFile will be "c:/abc" which is a directory and I am reading files and sub directories of sourceFile. Now in updateConnectorXML(sourceFile, destFile) I am just updating XML files.

Now I want to create same directory structure in another folder with updated XML files.

In above code the destePath is changed to only one directory and all files are going into the directory. How can I get back from that directory?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
Musaddique S
  • 1,539
  • 2
  • 15
  • 36
  • So basically you want to copy files to another directory? – We are Borg Sep 04 '15 at 13:48
  • possible duplicate of [Copy entire directory contents to another directory?](http://stackoverflow.com/questions/6214703/copy-entire-directory-contents-to-another-directory) – Peter Uhnak Sep 04 '15 at 14:23
  • I assume that you want the changes to be applied to your file system right?, even though your code says otherwise! – QuakeCore Sep 08 '15 at 14:01
  • basically in my code, what i am doing is. i am reading directory containing XML files and modifying the files and saving these files in another directory with same directory structure. – Musaddique S Sep 09 '15 at 09:48

1 Answers1

0

One fundamental error is the reuse of parameter destePath for holding the various subdirectory paths. Compare:

private static void readFiles(File sourceFolder, String destePath) {
    if (sourceFolder.isDirectory()){
        for (File sourceFile : sourceFolder.listFiles()) {
            if (sourceFile.isFile()) {
                File destFile = new File(destePath + "/"
                        + sourceFile.getName());
                updateConnectorXML(sourceFile, destFile);
            } else {
                String subPath = destePath + "/" + sourceFile.getName();
                File destDir = new File(subPath);
                destDir.mkdir();
                readFiles(sourceFile, subPath);
            }
        }
    }
}
laune
  • 31,114
  • 3
  • 29
  • 42