public String copyfile(String source, String destdir) {
String filename = "";
try {
String filepath = source;
File ofile = new File(filepath);
File syspath = new File(destdir);
if (syspath.exists()) {
Del_File(syspath.getAbsolutePath());
syspath.mkdirs();
}
else {
syspath.mkdirs();
}
File[] of = ofile.listFiles();
for (int i = 0; i < of.length; i++) {
File tf = of[i];
if (tf.isDirectory()) {
String dirname = tf.getName();
copyfile(tf.getAbsolutePath(), destdir + "/" + dirname);
}
else {
String name = tf.getName();
FileInputStream fis = new FileInputStream(tf);
FileOutputStream fos = new FileOutputStream(destdir + "/" + name);
byte[] tempch = new byte[fis.available()];
fis.read(tempch);
fos.write(tempch);
fos.flush();
fos.close();
fis.close();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return filename;
}
本文介绍了一个递归文件复制的方法,该方法能够实现源目录到目标目录的文件及子目录的完全复制。它首先检查目标目录是否存在,如果存在则清空并创建新的目录,不存在则直接创建。接着遍历源目录中的所有文件和子目录,对于子目录采用递归调用继续复制,对于文件则通过输入输出流完成复制。

4074

被折叠的 条评论
为什么被折叠?



