62,635
社区成员




import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
public class PathCopy {
/** 遍历文件夹时,针对遍历的对象进行回调访问 */
interface Visitor {
void handle(File file) throws IOException;
}
/** 1. 遍历文件夹 */
public static void scan(File path, Visitor visitor) throws IOException {
if (path.exists()) {
for (File file : path.listFiles()) {
visitor.handle(file);
if (file.isDirectory()) {
scan(file, visitor);
}
}
}
}
/** 2. 文件拷贝 */
public static void copy(File src, File des) throws IOException {
RandomAccessFile target = new RandomAccessFile(des, "rwd");
RandomAccessFile source = new RandomAccessFile(src, "r");
try (FileChannel out = target.getChannel();
FileChannel in = source.getChannel()) {
out.transferFrom(in, 0, src.length());
}
}
public static void pathCopy(File srcFolder, File desFolder) throws IOException {
if (!desFolder.exists()) {
desFolder.mkdirs();
}
final String basePath = srcFolder.getPath();
scan(srcFolder, file -> {
String parent = srcFolder.getParent();
String relative = parent.substring(basePath.length());
File target = new File(desFolder,relative + "/" + file.getName());
if (file.isDirectory()) {
target.mkdirs();
} else {
copy(file, target);
}
});
}
public static void main(String[] args) throws IOException {
final File srcFolder = new File("src/path");
final File desFolder = new File("des/path");
pathCopy(srcFolder, desFolder);
}
}
import java.io.*;
public class FileDirTest01 {
public static void main(String[] args) {
// // 拷贝文件夹目录
File in = new File("/users/lifeisgood/Documents/test");
// 拷贝目标(目录)
File out = new File("/users/lifeisgood/Documents/test2");
// 调用方法
CopyDir(in, out);
// File in = new File("/users/lifeisgood/Documents/test");
// System.out.println("getName="+in.getName());
// System.out.println("getAbsolutePath()="+in.getAbsolutePath());
}
/**
* 拷贝文件夹里面文件夹及文件,
* @param in 拷贝文件目录
* @param out 拷贝目标(目录)
*/
private static void CopyDir(File in, File out) {
if(in.isFile()) {
// srcFile如果是一个文件的话,递归结束。
// 是文件的时候需要拷贝。
// ....一边读一边写。
FileInputStream input = null;
FileOutputStream output = null;
try {
// 读这个文件
input = new FileInputStream(in);
// 写文件到新的目录为 out.getAbsolutePath()+"\\"+in.getName()
output = new FileOutputStream(out.getAbsolutePath()+"/"+in.getName());
byte[] bytes = new byte[1024*1024]; //一次复制1M
int readCount = 0;
/**
* 复制文件 一次复制1024*1024字节(1M)
*/
while((readCount = input.read(bytes)) != -1) {
output.write(bytes,0,readCount);
}
output.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
// 说明是文件夹,遍历这个文件夹
File[] files = in.listFiles();
for (File file: files) {
File newDir = new File(out.getAbsolutePath());
if (file.isDirectory()) { //判断是否为文件夹,是
// 新的目录为 out.getAbsolutePath() + "/" + in.getName()
newDir = new File(out.getAbsolutePath()+"/"+file.getName());
}
if (!newDir.exists()) { //判断是否含有此文件夹,否
newDir.mkdirs(); //创建子目录
}
System.out.println("in="+file.getAbsolutePath());
System.out.println("out="+newDir.getAbsolutePath());
// 递归方法CopyDir
CopyDir(file,newDir);
}
}
}