利用java复制文件或目录,要么使用递归的方法,要么使用walkFileTree的方法。大家可以比较一下。
package gy.finolo;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import static java.nio.file.FileVisitResult.CONTINUE;
public class CopyUtils {
public static void copy2Directory(Path inPath, Path outDirPath) throws IOException {
// 确保输出目录存在, 如果inPath是File的话, 这里就执行一次, 所以不用写到copyFile2Diretory()方法里
if (!Files.exists(outDirPath)) {
Files.createDirectories(outDirPath);
}
if (Files.isDirectory(inPath)) {
copyFilesInDirectory2Directory(inPath, outDirPath);
} else {
copyFile2Directory(inPath, outDirPath);
}
}
private static void copyFilesInDirectory2Directory(Path inDirPath, Path outDirPath) throws IOException {
DirectoryStream<Path> paths = Files.newDirectoryStream(inDirPath);
for (Path path : paths) {
if (Files.isDirectory(path)) {
Path subOutDirPath = outDirPath.resolve(inDirPath.relativize(path));
// 确保输出目录存在
if (!Files.exists(subOutDirPath)) {
Files.createDirectories(subOutDirPath);
}
copyFilesInDirectory2Directory(path, subOutDirPath);
} else {
Path subOutDirPath = outDirPath.resolve(inDirPath.relativize(path.getParent()));
copyFile2Directory(path, subOutDirPath);
}
}
}
private static void copyFile2Directory(Path inFilePath, Path outDirPath) throws IOException {
Path fileName = inFilePath.getFileName();
Path outFilePath = outDirPath.resolve(fileName);
int bufSize = 8 * 1024;
byte[] buffer = new byte[bufSize];
int len;
try (InputStream is = Files.newInputStream(inFilePath);
OutputStream os = Files.newOutputStream(outFilePath, StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE,
StandardOpenOption.CREATE)) {
while ((len = is.read(buffer)) > 0) {
os.write(buffer, 0, len);
}
}
}
/**
* 注意: visitFile方法中, 每次需要做一个if (inPath.equals(file))的判断,
* 如果inPath是文件, 那返回为true, 如果inPath为目录, 那永远返回false
* 我是为了统计, 才在里面加了一个判断, 在实际运用中, 有了这个判断效率就不高了。
* @param inPath
* @param outDirPath
* @throws IOException
*/
public static void copy2DirectoryV2(Path inPath, Path outDirPath) throws IOException {
// 确保输出目录存在
if (!Files.exists(outDirPath)) {
Files.createDirectories(outDirPath);
}
Files.walkFileTree(inPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
System.out.println("processing DIR: " + dir);
Path subOutDirPath = outDirPath.resolve(inPath.relativize(dir));
if (!Files.exists(subOutDirPath)) {
Files.createDirectories(subOutDirPath);
}
return CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
System.out.println("processing FILE: " + file);
Path outPath;
if (inPath.equals(file)) {
// 说明source是一个文件, 只需要单独复制一个文件即可
outPath = outDirPath.resolve(file.getFileName());
} else {
outPath = outDirPath.resolve(inPath.relativize(file));
}
Files.copy(file, outPath);
return CONTINUE;
}
});
}
public static void main(String[] args) {
Path inPath = Paths.get("/usr/local");
Path outDirPath = Paths.get("/opt");
try {
// CopyUtils.copy2Directory(inPath, outDirPath);
CopyUtils.copy2DirectoryV2(inPath, outDirPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}