Search code examples
javafilecopydirectorycopying

Copying children files of a source to a destination in Java


I'm trying to make a program that copies a directory's children, and I can't label all the specific names because they vary throughout each folder. Here's the code I have, but if the source is "C:\src" and the output is "C:\dst" it'll create the folder "C:\dst\src(children files)", but I want to make "C:\dst(children files)". Can anyone help?

public static void copy(File source, File destination) throws IOException {
    if (source == null) {
        throw new NullPointerException("Null Source");
    }
    if (destination == null) {
        throw new NullPointerException("Null Destination");
    }
    if (source.isDirectory()) {
        copyDirectory(source, destination);
    } else {
        copyFile(source, destination);
    }
}
//converts to location
public static void copyDirectory(File source, File destination) throws IOException {
    copyDirectory(source, destination, null);
}

public static void copyDirectory(File source, File destination, FileFilter filter) throws IOException {
    File nextDirectory = new File(destination, source.getName());
    if (!nextDirectory.exists() && !nextDirectory.mkdirs()) {// create the directory if necessary...
        Object[] filler = {nextDirectory.getAbsolutePath()};
        String message = "Dir Copy Failed";
        throw new IOException(message);
    }
    File[] files = source.listFiles();
    for (int n = 0; n < files.length; ++n) {// and then all the items below the directory...
        if (filter == null || filter.accept(files[n])) {
            if (files[n].isDirectory()) {
                copyDirectory(files[n], nextDirectory, filter);
            } else {
                copyFile(files[n], nextDirectory);
            }
        }
    }
}

public static void copyFile(File source, File destination) throws IOException {
    // what we really want to do is create a file with the same name in that dir
    if (destination.isDirectory()) {
        destination = new File(destination, source.getName());
    }
    FileInputStream input = new FileInputStream(source);
    copyFile(input, destination);
}

public static void copyFile(InputStream input, File destination) throws IOException {
    OutputStream output = null;
    try {
        output = new FileOutputStream(destination);
        byte[] buffer = new byte[1024];
        int bytesRead = input.read(buffer);
        while (bytesRead >= 0) {
            output.write(buffer, 0, bytesRead);
            bytesRead = input.read(buffer);
        }
    } catch (Exception e) {
        //
    } finally {
        input.close();
        output.close();
    }
    input = null;
    output = null;
}

Solution

  • Replace

    if (source.isDirectory()) {
        copyDirectory(source, destination);
    } else {
        copyFile(source, destination);
    }
    

    By

    if (source.isDirectory()) {
        for (File child : source.listFiles()) {
            if (child.isDirectory()) {
                copyDirectory(child, destination);
            } else {
                copyFile(child, destination);
            }
        }
    } else {
        copyFile(source, destination);
    }