如何从磁盘复制特定文件

时间:2014-02-03 21:45:43

标签: java linux photo

我正在寻找一个脚本,将特定文件从磁盘复制到另一个磁盘,丢弃所有重复的文件

例如.jpg,.tiff,.nef

我的所有照片都有一个非常混乱的备份,我想将所有照片复制到其他磁盘上避免重复的照片

1 个答案:

答案 0 :(得分:1)

如果您使用的是Java 7,则可以使用Files类。

File source = new File("sourcePath");
File dest = new File("destPath");
Files.copy(source.toPath(), dest.toPath());

您可以通过收集名称来检查重复项(除非您有多个具有相同名称的文件)。

ArrayList<String> names = new ArrayList<>();
for(File f in dir) {
    if(!names.contains(f.path) {
        // copy the file using the code above
        names.add(f.path);
    }
}

如果您的逻辑对于确定重复更复杂,请将f.path替换为更强大的内容。这是一个让你开始的例子。

从路径名中删除扩展名:

String pathName = f.path.substring(0, f.path.lastIndexOf('.'));
相关问题