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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 莫泊桑leon 中级黑马   /  2014-9-5 13:36  /  816 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

复制文件夹思路是:先实现文件的复制,然后遍历文件夹下的文件进行复制
代码如下:
  1. package sample;

  2. import java.io.*;
  3. import java.nio.ByteBuffer;
  4. import java.nio.channels.FileChannel;

  5. class CopyFile{
  6.         protected File sourceF;
  7.         protected File targetF;
  8.         CopyFile(File sourceF,File targetF){
  9.                 this.sourceF=sourceF;
  10.                 this.targetF=targetF;
  11.         }
  12.         @SuppressWarnings("resource")
  13.         //文件的复制
  14.         public  void Copy() throws IOException{
  15.                 if(targetF.isDirectory())
  16.                 {
  17.                         targetF.mkdirs();
  18.                         //在目标文件夹中建立和目标文件同名的文件
  19.                         targetF=new File(targetF.getAbsolutePath()+"\\"+sourceF.getName());
  20.                        
  21.                 }
  22.                 FileChannel in = new FileInputStream(sourceF).getChannel();//得到输入通道      
  23.                 FileChannel out = new FileOutputStream(targetF).getChannel();//得到输出通道  
  24.         ByteBuffer buffer = ByteBuffer.allocate(1024);//设定缓冲区  
  25.         while(in.read(buffer) != -1){  
  26.             buffer.flip();//准备写入,防止其他读取,锁住文件  
  27.             out.write(buffer);  
  28.             buffer.clear();//准备读取。将缓冲区清理完毕,移动文件内部指针  
  29.         }
  30.         in.close();
  31.         out.close();
  32.         }
  33. }
  34. class CopyDirectory {
  35.         private String sourceD;
  36.         private String targetD;
  37.         CopyDirectory(String sourceD, String targetD) {
  38.                 this.sourceD=sourceD;
  39.                 this.targetD=targetD;               
  40.         }
  41.         //文件夹的复制
  42.         public void Copy() throws IOException{
  43.                 //System.out.println(sourceD);
  44.                 String[] sF=new File(sourceD).list();
  45.                 String sTemp=targetD+"\\"+new File(sourceD).getName();//在目标文件夹中建立和源文件同名文件夹
  46.                 File tF=new File(sTemp);
  47.                 tF.mkdirs();
  48.                 for (int i = 0; i < sF.length; i++){
  49.                         if(new File(sourceD+"\\"+sF[i]).isFile())
  50.                         {
  51.                                 System.out.println(sourceD+"\\"+sF[i]+2);
  52.                                 File sourceF=new File(sourceD+"\\"+sF[i]);
  53.                                 File targetF=tF;
  54.                                 new CopyFile(sourceF,targetF).Copy();
  55.                         }
  56.                         else if(new File(sourceD+"\\"+sF[i]).isDirectory())
  57.                         {
  58.                                 sourceD=sourceD+"\\"+sF[i];
  59.                                 System.out.println(sourceD+1);
  60.                                 targetD=sTemp;
  61.                                 new CopyDirectory(sourceD,targetD).Copy();
  62.                         }
  63.                 }
  64.                
  65.                 //File[] file=new File(sourceD).listFiles();
  66.                
  67.         }
  68. }
  69. public class Sample2 {

  70.         public static void main(String[] args) throws IOException {
  71.                 // TODO Auto-generated method stub
  72.          String sourceD="C:\\新建文件夹";
  73.          String targetD="E:\\新建文件夹";
  74.          new CopyDirectory(sourceD,targetD).Copy();
  75.         }

  76. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马