使用java将文件从父目录移动到子目录

使用java将文件从父目录移动到子目录,java,Java,您好,我正在尝试将文件和文件夹从根文件夹复制到高达5级的子文件夹。如果需要添加文件夹或文件,则必须执行相同的操作 我尝试了一些关于参数化级别的示例,能够列出目录,并建议我获取输出文件夹、文件结构的方法 public static void main(String[] args) throws IOException { String path = "Workspace/MainFolder/"; String targetPath="/Works

您好,我正在尝试将文件和文件夹从根文件夹复制到高达5级的子文件夹。如果需要添加文件夹或文件,则必须执行相同的操作

我尝试了一些关于参数化级别的示例,能够列出目录,并建议我获取输出文件夹、文件结构的方法

public static void main(String[] args) throws IOException {

     String path = "Workspace/MainFolder/";
    String targetPath="/Workspace/Dest/";
    Files.walk(Paths.get(path),5).forEach(p->{
        if(p.toFile().isDirectory()){
            Stream.of(new File(path).listFiles()).filter(File::isFile).map(f->new File(f.getPath().replace(path,targetPath))).forEach(f->{
                f.getParentFile().mkdirs();
                try {
                    Files.copy(p,f.toPath(),StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            });
        }else{
            try {
                Files.copy(p,new File(p.toFile().getPath().replace(path,targetPath)).toPath(),StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
  }
 }

这个解决方案怎么样

包装q63242312; 导入java.io.File; 导入java.io.IOException; 导入java.nio.file.Files; 导入java.nio.file.Path; 导入java.nio.file.path; 导入java.nio.file.StandardCopyOption; 导入java.util.stream.stream; 公共类RecCopyWithRoot{ 私有静态布尔deleteDirectoryFile directoryToBeDeleted{ 文件[]allContents=directorytobedeletted.listFiles; 如果allContents!=null{ 对于文件:allContents{ 删除目录文件; } } 返回directorytobedelete.delete; } 公共静态无效字符串[]args{ 字符串from=./from; 字符串to=./to; 试一试{ File srcFile=新文件from.toPath.toabsolutionpath.toFile; File targetFile=new Fileto.toPath.toabsolutionpath.toFile; 如果!deleteDirectorytargetFile{ //抛出新的IllegalStateException无法删除输出目录; } copyFoldersrcFile.toPath,targetFile.toPath; 尝试Stream=Files.walksrcFile.toPath,5{ stream.filterp->p.toFile.isDirectory.forEachdir->{ 路径tmpDir=dir; //System.err.printlntmpDir; whiletmpDir=tmpDir.getParent.toFile.getAbsolutePath.startsWithRCFile.getAbsolutePath{//TODO同时具有父级 对于文件f:tmpDir.toFile.listFiles{//TODO srcFile.listFiles->从根目录到目录的全部 iff.isFile{ System.out.printlnres:+dir+|+srcFile+|+f; System.out.printlnf.getName+->+targetFile.toPath.resolvesrcFile.toPath.relativizef.toPath; 试一试{ Files.copyf.toPath,新文件targetfile.toPath.resolvescfile.toPath.relatilizedir.toFile,f.getName.toPath,StandardCopyOption.REPLACE_现有; }捕捉异常{ //TODO自动生成的捕捉块 e、 打印跟踪; } } } } }; } 对于要删除的文件:targetFile.listFiles{ toDelete.delete; } }捕捉异常{ //TODO自动生成的捕捉块 e、 打印跟踪; } } 公共静态void copyFolderPath src,Path dest引发IOException{ System.out.printlnsrc+->+dest; 如果src.toFile.isDirectory{ try Stream=Files.walksrc{ stream.forEachsource->{ 路径newDest=dest.resolvesrc.relativizesource; 如果!newDest.toFile.存在{ 试一试{ Files.copysource、newDest、StandardCopyOption.REPLACE\u现有文件; }捕捉异常{ e、 打印跟踪; } } }; } }否则{ //Files.copysrc、dest、StandardCopyOption.REPLACE_现有文件; } } }
是否要覆盖现有文件?files.walkpath.getpath,5.filterp->p.toFile.isDirectory.forEachp->Stream.ofnew Filepath.listFiles.forEachf->files.copyp,f.toPath是否有效?就在路径声明之后。您甚至不应该需要其他代码。我收到以下错误:java.nio.file.filealreadyexistException:Workspace/MainFolder/.DsStore和java.nio.file.filealreadyexistException:Workspace/MainFolder/folder您需要启用覆盖现有文件,如:files.walkpath.getpath,5.filterp->p.toFile.isDirectory.forEachp->Stream.of new Filepath.listFiles.forEachf->Files.copyp、f.toPath、copypoption.REPLACE\u EXISTONG.Comments不用于扩展讨论;这段对话已经结束。