黑马程序员技术交流社区
标题:
关于复制文件夹及其文件夹下的文件
[打印本页]
作者:
fjl_fight
时间:
2013-5-8 11:13
标题:
关于复制文件夹及其文件夹下的文件
public class Demo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
copyFolder(new File("F:\\test"),new File("D:\\fest"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 复制文件夹及其文件
* @param src
* @param dest
* @throws IOException
*/
private static void copyFolder(File src, File dest) throws IOException {
//如果源文件是为文件夹
if (src.isDirectory()) {
//如果目标文件夹不存在
if (!dest.exists()) {
//创建文件夹
dest.mkdir();
}
//列出此文件夹下的文件及目录
String files[] = src.list();
for (String file : files) {
//创建一以源文件src和file文件名的一个新实例
File srcFile = new File(src, file);
// System.out.println(srcFile);
File destFile = new File(dest, file);
// System.out.println(destFile);
// 递归复制
copyFolder(srcFile, destFile);
}
} else { //如果源文件不是为文件夹
//取得输入流
InputStream in = new FileInputStream(src);
//取得输出流
OutputStream out = new FileOutputStream(dest);
//声明byte数组
byte[] buffer = new byte[1024];
int length;
//循环读写
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
//资源关闭
in.close();
out.close();
}
}
}
复制代码
作者:
曹睿翔
时间:
2013-5-8 14:55
这次怎么不领任务了
作者:
fjl_fight
时间:
2013-5-8 15:26
要开始写毕业论文呢,没时间啊
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2