读书人

java copy 索引 文件 其它写法

发布时间: 2012-10-28 09:54:44 作者: rapoo

java copy 目录 文件 其它写法
public static void copyDir(final File src, final File dest) throws IOException {
dest.mkdirs();
File[] files = src.listFiles();

int j = files.length; // cache the length so it doesn't need to be looked up over and over in the loop
for (int i = 0; i < j; i++) {
File file = files[i];
if (file.isDirectory()) {
copyDir(file, new File(dest, file.getName()));
} else {
copyFile(file, new File(dest, file.getName()));
}
}
}

public static void copyFile(final File src, final File dest) throws IOException {
dest.getParentFile().mkdirs();
dest.createNewFile();

FileChannel sourceChannel = new FileInputStream(src).getChannel();
FileChannel targetChannel = new FileOutputStream(dest).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
sourceChannel.close();
targetChannel.close();
}

读书人网 >编程

热点推荐