java - 将源的子文件复制到 Java 中的目标

标签 java file copy directory copying

我正在尝试制作一个复制目录子项的程序,但我无法标记所有特定名称,因为它们在每个文件夹中各不相同。这是我的代码,但如果源是“C:\src”并且输出是“C:\dst”,它将创建文件夹“C:\dst\src(子文件)”,但我想制作“C:\dst(子文件)”。谁能帮忙?

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;
}

最佳答案

替换

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

通过

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

关于java - 将源的子文件复制到 Java 中的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13869366/

相关文章:

java - GarbageCollectorMXBean Full GC CollectionCount 和 CollectionTime

java - 回调怪异行为(Android、Picasso 库)

java - 为什么实例字段的 "int someVal=0"不被视为死代码?

java - java jar文件输出上的php exec是空数组

c - 在C代码中读取同一个文件两次

php - 如何使用 PHP 复制 mysql 记录并使用不同的 id 保存?

c# - File.Move 和 File.Copy 给出 IOException 登录失败

C - 在系统函数调用后将 SHELL 输出保存在文件中

c# - SaveFileDialog 上的自定义检查文件名

javascript - 通过单击将 css 类附加到多个 div