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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© CHENVICTORY 中级黑马   /  2015-10-11 21:38  /  363 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  学习了文件的复制,怎么进行文件夹的复制?  

2 个回复

倒序浏览
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;

  7. public class fuzhiFile {
  8.         public static void main(String[] args) {
  9.                 File file = new File("D:\\myfile");
  10.                 String filepath="E:\\";
  11.                 search(file,filepath);
  12.                
  13.         }
  14.         public static void search(File file,String filepath){
  15.                 if(file.exists()){
  16.                         if(file.isDirectory()){
  17.                                 File file2 = new File(filepath,file.getName());
  18.                                 file2.mkdirs();
  19.                                 File[] files = file.listFiles();
  20.                                 for(File f:files){
  21.                                         search(f,file2.getAbsolutePath());
  22.                                 }
  23.                         }else{
  24.                                 fuzhiFiles(file,filepath);
  25.                         }
  26.                 }
  27.         }
  28.         private static void fuzhiFiles(File file, String filepath) {
  29.                 BufferedInputStream bi= null;
  30.                 BufferedOutputStream bo= null;
  31.                 byte[] buf= new byte[1024*1024];
  32.                 int len = -1;
  33.                 try {
  34.                         bi = new BufferedInputStream(new FileInputStream(file));
  35.                         bo = new BufferedOutputStream(new FileOutputStream(new File(filepath,file.getName())));
  36.                         while((len =bi.read(buf))!=-1){
  37.                                 bo.write(buf,0,len);
  38.                                 bo.flush();
  39.                         }
  40.                 } catch (IOException e) {
  41.                         e.printStackTrace();
  42.                 }finally{
  43.                         if(bi!=null)
  44.                                 try {
  45.                                         bi.close();
  46.                                 } catch (IOException e) {
  47.                                         e.printStackTrace();
  48.                                 }finally{
  49.                                         if(bo!=null)
  50.                                                 try {
  51.                                                         bo.close();
  52.                                                 } catch (IOException e) {
  53.                                                         e.printStackTrace();
  54.                                                 }
  55.                                 }
  56.                 }
  57.         }
  58. }
复制代码


抛砖引玉
回复 使用道具 举报
如果能加点注释就好了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马