0

i want to copy files from parent directory into subfolder in parent directory. Now i get the copied files into subfolder, but it repeated itself everytime if i get already the subfolder and files copied, it makes it all time repeatedly, i want it to male only one time

public static void main(String[] args) throws IOException {     
    File source = new File(path2);
    File target = new File("Test/subfolder");
    copyDirectory(source, target);
}
public static void copyDirectory(File sourceLocation, File targetLocation)
        throws IOException {
    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }
        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]), new File(
                    targetLocation, children[i]));
        }
    } else {
        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);
        byte[] buf = new byte[1];
        int length;
        while ((length = in.read(buf)) > 0) {
            out.write(buf, 0, length);
        }
        in.close();
        out.close();

    }
}
usha
  • 28,973
  • 5
  • 72
  • 93
user3638596
  • 15
  • 1
  • 4

2 Answers2

0

You are calling your method recursively without a condition to break the recursion. You will have to exclude directories in your for-loop.

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
0

You program have problem in following line

String[] children = sourceLocation.list();

Lets suppose your parent dir = Test So the following code will create a sub-folder under test

if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

And after that you are retrieving the children of source folder as your destination is already created it will also be counted as child of source folder and recursively get copied. So you need to retrieve children first and then create the target directory So that target directory would not be count in copy process. Change your code as follows.

public static void main(String[] args) throws IOException {     
        File source = new File("Test");
        File target = new File("Test/subfolder");
        copyDirectory(source, target);
    }
    public static void copyDirectory(File sourceLocation, File targetLocation)
            throws IOException {

        String[] children = sourceLocation.list();
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]), new File(
                        targetLocation, children[i]));
            }
        } else {
            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);
            byte[] buf = new byte[1];
            int length;
            while ((length = in.read(buf)) > 0) {
                out.write(buf, 0, length);
            }
            in.close();
            out.close();

        }
    }
Sumit Tyagi
  • 431
  • 2
  • 14