Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取目录并创建相同的结构_Java - Fatal编程技术网

Java 读取目录并创建相同的结构

Java 读取目录并创建相同的结构,java,Java,我想读取一个目录,并在另一个目录中创建相同的文件和子目录 目录的路径将是用户定义的值 private static void readFiles(File sourceFolder, String destePath) { if (sourceFolder.isDirectory()) for (File sourceFile : sourceFolder.listFiles()) { if (sourceFile.isFi

我想读取一个目录,并在另一个目录中创建相同的文件和子目录

目录的路径将是用户定义的值

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

            }
    }
这里的e.d源文件将是“c:/abc”,这是一个目录,我正在读取源文件的文件和子目录。现在,在
updateConnectorXML(sourceFile,destFile)
中,我正在更新XML文件

现在我想在另一个文件夹中用更新的XML文件创建相同的目录结构


在上述代码中,
destePath
仅更改为一个目录,所有文件都将进入该目录。如何从该目录返回?

一个基本错误是重用参数destePath来保存不同的子目录路径。比较:

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

因此,基本上您想将文件复制到另一个目录?可能与我假设您希望将更改应用到您的文件系统中,对吗?即使您的代码中另有说明!基本上在我的代码中,我正在做的是。我正在读取包含XML文件的目录,修改这些文件,并将这些文件保存在具有相同目录结构的另一个目录中。