目录拷贝
import java.io.*;
public class CopyAll {
public static void main(String[] args) {
File srcFile = new File("D:\\StudySource\\Java\\powernode\\文本笔记");
File destFile = new File("F:\\Java 目录拷贝\\");
copyAll(srcFile, destFile);
}
private static void copyAll(File srcFile, File destFile) {
if (srcFile.isFile()) {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(srcFile);
byte[] bytes = new byte[1024 * 1024];
out = new FileOutputStream((destFile.getAbsolutePath().endsWith("\\") ?
destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\" )
+ srcFile.getAbsolutePath().substring(3));
int readCount = 0;
while((readCount = in.read(bytes)) != -1){
out.write(bytes,0, readCount);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in == null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
File[] files = srcFile.listFiles();
for (File f : files) {
if (f.isDirectory()){
String srcDir = f.getAbsolutePath();
String srcDir2 = srcDir.substring(3);
String destDir = (destFile.getAbsolutePath().endsWith("\\") ?
destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\" )
+ srcDir2;
File newFile = new File(destDir);
if (!newFile.exists()){
newFile.mkdirs();
}
}
copyAll(f, destFile);
}
}
}