如何将一个文件夹里所有的内容拷贝?

hhuzhj 2003-04-10 05:15:52
请问各位高手,我现在需要将一个文件夹里面的所有东西拷贝到另外一个硬盘中,该如何用程序实现。如将c:\tomcat中所有的内容拷贝到d盘中。我是一个新手,请多多帮忙,现在我知道大概的流程,先遍历这个文件夹,然后用文件流实现拷贝,但是具体的我不知道该怎么做。高分求取源程序,请给一个源程序。在线等待。成功即给分。
...全文
136 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bjzhanghao 2003-04-11
  • 打赏
  • 举报
回复
稍微改一下就可以了,完整代码如下:
import java.io.*;

public class Application3 {
boolean packFrame = false;

public Application3() {
copyDir(new File("c:/temp/"),new File("d:/temp/"));
}

public void copyDir(File src_path,File dest_path){
if(!dest_path.exists())
dest_path.mkdirs();
File[] files = src_path.listFiles();
for(int i=0;i<files.length;i++){
File src=files[i];
File dest=new File(dest_path.getPath()+File.separator+files[i].getName());
if(!src.isDirectory())
copyFile(src,dest);
else
copyDir(src,dest);
}
}

public void copyFile(File src,File dest){
try{
System.out.println(src.getAbsoluteFile()+" -> "+dest.getAbsoluteFile());
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length = -1;
while((length=in.read(buffer))!=-1){
out.write(buffer,0,length);
}
out.flush();
out.close();
in.close();
System.out.println("文件复制成功!");
}catch(Exception e){
System.out.println("文件复制失败!");
}
}

public static void main(String[] args) {
new Application3();
}
}
chili1979 2003-04-11
  • 打赏
  • 举报
回复
可以稍微改动一下啊。
如果是文件夹,在目标目录里建立文件夹,然后再拷贝到该文件夹。
hhuzhj 2003-04-11
  • 打赏
  • 举报
回复
我试过你的程序了,但是好像你的程序和我的要求不太一样,我要将c:\所有的文件和文件夹都要拷贝到d:\temp里,就是里面的所有的内容都要拷贝,但是你的程序好像只能拷贝文件,不能拷贝文件夹哦!!
hhuzhj 2003-04-11
  • 打赏
  • 举报
回复
我好像用的不行阿!
hhuzhj 2003-04-11
  • 打赏
  • 举报
回复
谢谢楼上的!!散分!!
lynx800602 2003-04-10
  • 打赏
  • 举报
回复
楼上的高手写的没错
我验证了!
好好学习吧!
我也在学习!
快给她分!
bjzhanghao 2003-04-10
  • 打赏
  • 举报
回复
我写了个application的,把c:\的文件拷到d:\temp里,你看看
import java.io.*;

public class Application3 {
boolean packFrame = false;

public Application3() {
copyDir(new File("c:/"),new File("d:/temp/"));
System.out.println(new File("d:/temp/").getPath()+File.separator);
}

public void copyDir(File src_path,File dest_path){
File[] files = src_path.listFiles();
for(int i=0;i<files.length;i++){
File src=files[i];
File dest=new File(dest_path.getPath()+File.separator+files[i].getName());
if(!src.isDirectory())
copyFile(src,dest);
}
}

public void copyFile(File src,File dest){
try{
System.out.println(src.getAbsoluteFile()+" -> "+dest.getAbsoluteFile());
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length = -1;
while((length=in.read(buffer))!=-1){
out.write(buffer,0,length);
}
out.flush();
out.close();
in.close();
System.out.println("文件复制成功!");
}catch(Exception e){
System.out.println("文件复制失败!");
}
}

public static void main(String[] args) {
new Application3();
}
}
hhuzhj 2003-04-10
  • 打赏
  • 举报
回复
急阿,请大家帮忙阿!
分不够的话可以再加的!!
hhuzhj 2003-04-10
  • 打赏
  • 举报
回复
请大家帮忙阿!!

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧