网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

Java实现文件拷贝(包括文件夹下的子文件夹和其中的文件)

http://www.diybl.com/ 2008-2-21  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

import java.io.*;

public class CopyAll {

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

 }

 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];
   while (in.read(buffer) != -1) {
    out.write(buffer);
   }
   out.close();
   in.close();
   System.out.println("文件拷贝成功");
  } catch (Exception e) {
   System.out.println("文件拷贝失败");
  }

 }

 public static void main(String[] args) {
  CopyAll t = new CopyAll();
  t.copyDir(new File(args[0]), new File(args[1]));

 }



欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言