/**
* @author ycw
* @version v1.0
* @date 2022/3/17 16:15
* @description
*/
public class TestFilesWalkFileTree {
public static void main(String[] args) throws IOException {
String source = "E:\\opt";
String target = "E:\\ept";
Files.walk(Paths.get(source)).forEach(path -> {
try {
String targetName = path.toString().replace(source,target);
//如果是目录
if(Files.isDirectory(path)){
Files.createDirectories(Paths.get(targetName));
}
//是文件
else if(Files.isRegularFile(path)){
Files.copy(path,Paths.get(targetName));
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
Files.walk实现优雅拷贝多级目录。值的注意的是Files.walk(Paths.get(source))拿到的是stream流,里面的流对象代表我们正在遍历的目录或者文件。