复制一个文件夹中的所有文件和文件夹的java程序实现

使用java程序实现了对文件夹的复制功能:

 package demo.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Date;

public class TestIo {
 public static float length = 0; 
 s public  void isFile(File f ,String dir) throws IOException{ //使用递归的方法遍历该目录下的所有文件和文件夹
  File[] farry = f.listFiles(); //得到该文件夹下的所有目录和文件
  for(int i=0;i<farry.length;i++){
   String fileName = farry[i].getName();
   String filePath = farry[i].getPath();
   String path = changPath(filePath);   //将路径中的\替换为/
   String newPath = newPath(path, dir);   //建立目标文件路径
   if(farry[i].isDirectory()){  //判断是否是目录文件
    boolean b = createFile(newPath);  //创建与该文件夹同名的目标文件中的文件夹 创建成功返回true失败返回false
    System.out.println(fileName);
       System.out.println(filePath);
       System.out.println(newPath);
       System.out.println(b);
    System.out.println("目录名:"+farry[i].getName() +" : 路径: "+farry[i].getPath());  //打印该目录的名称和路径
    isFile(farry[i],dir);        //递归调用该方法
   }else{  //如果不是目录文件复制该文件到指定的路径上
    copyFile(path, newPath); //将该文件复制到目标文件夹中
    totalLength(farry[i].length()/1024);
    System.out.println("文件名:"+farry[i].getName()+" :路径: "+farry[i].getPath()+" : 文件大小:"+farry[i].length()/1024+"KB");
   }
   
   
  }
  
 }
 
 public String changPath(String path){  //修改路径
  path = path.replace("\\","/");
  return path;
 }
 public String newPath(String path,String dir){  //创建新的目录地址
  String newPath = path.substring(path.indexOf(":")+1);
  newPath = dir+newPath;
  return newPath;
 }
 public boolean createFile(String path) throws IOException{   //创建文件夹
  File f = new File(path);
  boolean flag = false;
  // f.createNewFile();
  flag =  f.mkdir();
  if(f.isDirectory()){
   flag = true;
  }
  return flag;
 }
 public void copyFile(String inPath,String outPath){ //复制文件夹中的文件到目标文件夹
  try {
   BufferedReader br = new BufferedReader(new FileReader(inPath));
   BufferedWriter bw = new BufferedWriter(new FileWriter(outPath));
   String len = br.readLine();
   while (len != null) {
    bw.write(len);
    len = br.readLine();
   }
   br.close();  //关闭输入流
   bw.close();  //关闭输出流
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void firstCopy(File f, String dir){  //在复制之前建立目标文件夹中的根目录 如果该文件不是文件夹则直接复制该文件
  String path = f.getPath();
  path = changPath(path);
  String newPath = newPath(path, dir);
  if(f.isDirectory()){
   try {
    boolean flag = createFile(newPath);
    isFile(f,dir); //遍历该文件夹
   } catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("路径不正确");
    e.printStackTrace();
   }
  }else{ //不是文件夹则直接复制该文件
   copyFile(path, newPath);
  }
  
 }
 public float totalLength(float f){ //计算该文件夹中所有文件的总长度
  length = length+f;
  return length;
 };
 public static void main(String[] args) throws IOException {
 
  TestIo t = new TestIo();
  Date date = new Date();
  long start = 0;
  long end = 0;
  start = date.getTime();
     File f = new File("F:/11");  //需要拷贝的文件或文件夹路径
     t.firstCopy(f, "E:/22");         //目标路径
     Date d2 = new Date();
     end = d2.getTime();
     System.err.println("总用时为:"+(end-start));
     System.err.println("复制文件的总大小为:"+length/1024+" MB");
    // t.doCopy2();
 }

}

遍历是使用递归实现

注释写的挺详细的,相信大家直接看注释也看以看的差不多,如果有什么疑问可以随时提出,方便大家一起交流。

posted @ 2011-08-12 14:41  村里的树  阅读(2015)  评论(3编辑  收藏  举报