A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 段培峰 中级黑马   /  2015-7-23 11:13  /  539 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.FilenameFilter;
  7. import java.io.IOException;
  8. /*
  9. * 编写一个程序,将d:\中的内容复制到e:\中。
  10. */
  11. public class Test12
  12. {
  13.          public static void main(String[] args)throws IOException{
  14.                 //封装源文件
  15.                 File srcFile = new File("E:\\");
  16.                 //封装目的文件
  17.                 File destFile = new File("F:\\");
  18.                 //复制文件
  19.                 copy(srcFile,destFile);
  20.          }

  21.          public static void copy(File srcFile,File destFile)throws IOException{
  22.                 if(!destFile.exists())
  23.                         destFile.mkdir();

  24.                 //获取符合条件的文件数组
  25.                 File[] arrFile = srcFile.listFiles();

  26.                 //遍历源文件
  27.                 for(File file : arrFile){
  28.                         String name = file.getName();
  29.                         File newFile = new File(destFile,name);
  30.                         if(file.isDirectory()){
  31.                                 copy(file,newFile);
  32.                         }else{
  33.                                 //复制
  34.                                 BufferedReader br = new BufferedReader(new FileReader(file));
  35.                                 BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
  36.                                 String line = null;
  37.                                 while((line = br.readLine())!=null){
  38.                                         bw.write(line);
  39.                                         bw.newLine();
  40.                                         bw.flush();
  41.                                 }
  42.                                 bw.close();
  43.                                 br.close();
  44.                         }
  45.                 }
  46.                
  47.          }
  48. }
复制代码

2 个回复

倒序浏览
好东西,昨晚就在想这个
回复 使用道具 举报
学习了,很有用赞36个
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马