//无论你是否回答了此问题,小弟先感谢你对本人,本问题的关注,谢谢!!package com.ym2005.copy;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
//这个是我的类,用来把一个文件夹从一个地方复制到另外一个地方。..
public class Mycopy {
//这个方法是用来复制文件的测试可以成功复制
public static void copyFile(File source, File target) throws Exception {
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(target);
byte[] a = new byte[2000];
int len = in.read(a);
while (len != -1) {
out.write(a, 0, len);
len = in.read(a);
}
}
//这个方法是用来复制文件夹(包括里面的文件)。-问题是创建不了文件夹.问题就出现在这里...
public static void copyFolder(File source, File target) throws Exception {
System.out.println(source.getAbsolutePath());
if (source.isDirectory()) {
if (source.isFile()) {
copyFile(source, target);
System.out.println("是文件");
} else {
System.out.println("是目录");
source.mkdirs();
}
File[] a = source.listFiles();
for (int i = 0; i < a.length; i++) {
copyFolder(a[i], target);
}
}
} public static void main(String[] args) {
//这里是定义了目标文件夹和源文件夹
File f1 = new File("D:\\a");
File f2 = new File("C:\\");
try {
copyFolder(f1, f2);
} catch (Exception e) {
e.printStackTrace();
}
}}

解决方案 »

  1.   


    // 把拷贝文件,如果新文件不存在,自动创建
    void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);// Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    }
    in.close();
    out.close();
    }// 调用这个方法
    // 递归拷贝文件夹,如果新文件夹不存在则自动创建。
    public void copyDirectory(File srcDir, File dstDir) throws IOException {
    if (srcDir.isDirectory()) {
    if (!dstDir.exists()) {
    dstDir.mkdir();
    }String[] children = srcDir.list();
    for (int i=0; i<children.length; i++) {
    copyDirectory(new File(srcDir, children[i]),
    new File(dstDir, children[i]));
    }
    } else {
    // 这个就是一楼给出的方法
    copy(srcDir, dstDir);
    }
    }
      

  2.   

    楼主的方法错误在于,需要判断是否存在再去创建文件夹
    具体看我给的代码中的exists处
      

  3.   

    package test;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    //这个是我的类,用来把一个文件夹从一个地方复制到另外一个地方。..
    public class aaaa {
            //这个方法是用来复制文件的测试可以成功复制
            public static void copyFile(File source, File target) throws Exception {
                    FileInputStream in = new FileInputStream(source);
                    FileOutputStream out = new FileOutputStream(target+"\\"+source.getName());
                    System.out.println(source.getName());
                    byte[] a = new byte[2000];
                    int len = in.read(a);
                    while (len != -1) {
                            out.write(a);
                            len = in.read(a);
                    }
                    
                    out.flush();
                    out.close();
                    in.close();
            }
            //这个方法是用来复制文件夹(包括里面的文件)。-问题是创建不了文件夹.问题就出现在这里...
            public static void copyFolder(File source, File target) throws Exception {
                    System.out.println(source.getAbsolutePath());
                    if (source.isDirectory()) {
                            if (source.isFile()) {
                                    copyFile(source, target);
                                    System.out.println("是文件");
                            } else {
                                    System.out.println("是目录");
                                    target.mkdir();
                            }
                            File[] a = source.listFiles();
                            for (int i = 0; i < a.length; i++) {
                                   if (a[i].isDirectory())
                                   {
                                        copyFolder(a[i], new File(target.getAbsolutePath()+"\\"+a[i].getName()));
                                   }else{
                                       copyFile(a[i],target);
                                   }
                            }
                    }
            }        public static void main(String[] args) {
    //这里是定义了目标文件夹和源文件夹
                    File f1 = new File("E:\\Java项目相关代码");
                    File f2 = new File("d:\\Java项目相关代码");
                    try {
                            copyFolder(f1, f2);
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
            }}
      

  4.   

    /**
      * 安装程序,主要进行文件的拷贝.
      * 
      */
     private void installFiles(String src,String dest) throws InstallException{
      File file1=new File(srcFold,src);
      File file2=new File(destFold,dest);
      if(file1.isFile()){//判断是否是文件对象
      log.info(file2.getPath());
      FileCopy.copyByFile(file1,file2);
      }else if(file1.isDirectory()){
       if(!file2.exists())//如果文件夹不存在则创建 
      file2.mkdir();
          log.info(file2.getPath());
          File[] fs=file1.listFiles();//遍历整个文件夹
       for(int i=0;i<fs.length;i++){
      String strPath=fs[i].getPath().substring(srcFold.length()+1);
      installFiles(strPath,strPath);//递归调用
       }
      }else{
       throw new InstallException("不存在该文件或目录!");
      }
     }
      

  5.   

    if   (source.isDirectory())   {
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~好像多了个右括号, 这里是判断是目录??下面又判断是文件??!
    if   (source.isFile())   {
    copyFile(source,   target);
    System.out.println( "是文件 ");
    }   else   {
    System.out.println( "是目录 ");
    source.mkdirs();
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~建元目录?已经存在了,应该是:target.mkdirs();
    }
    File[]   a   =   source.listFiles();
    for   (int   i   =   0;   i   <   a.length;   i++)   {
    copyFolder(a[i],   target);
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~如果目录结构一样的话,就应该是target的子目录了.
    }
    }
      

  6.   

    package com.wepull.io.lesson01.homework;import java.io.*;public class hk4 {
    public void fun(String s1,String s2){
    File f1=new File(s1);//先将传进来的两个目录转换为File对象
    File f2=new File(s2);
    hk5 hk=new hk5();//事先做了个复制的方法,就直接拿来用了,方法在下面
    File[]f=f1.listFiles();//将源目录的文件生成一个File类型的数组
    if(f.length==0){//如果数组的长度为空,即目录为空,则直接创建目标目录即可{f3.mkdirs();}
    File f3=new File(s2);
    f3.mkdirs();
    }
    for (int i = 0; i < f.length; i++) {//如果数组的长度不为空,即目录不空,
    if(f[i].isFile()){//如果第i个文件是文件,则调用方法复制文件
    hk.copyfile(s1+"/"+f[i].getName(),s2+"/"+f[i].getName());
    }else if(f[i].isDirectory()){//如果第i个文件是目录,先创建目标目录即可{f3.mkdirs();}
    String s3=s2+"/"+f[i].getName();
    File f3=new File(s3);
    f3.mkdirs();
    fun(f[i].getPath(),s2+"/"+f[i].getName());//然后将该目录和目标创建好的目录作为源目录和目标目录调用方法
    }
    }
    }
    public void fun2() {

    }
    public static void main(String[] args) {
    hk4 hk=new hk4();
    String s1="e:/io";
    String s2="e:/oi";
    hk.fun(s1, s2);
    }
    }
    class hk5 {
    /**
     * 用BufferedReader和PrintStream来复制
     */
    public void copyfile(String s1,String s2) {
    try {
    BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(s1)));
    PrintStream ps=new PrintStream(new FileOutputStream(s2));
    String len;
    while((len=br.readLine())!=null){
    ps.println(len);
    }
    br.close();
    ps.flush();
    ps.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } }
    }
      

  7.   

    public   static   void   copyFolder(File   source,   File   target)   throws   Exception   { 
    System.out.println(source.getAbsolutePath()); 
    if   (source.isDirectory())   { 
    if   (source.isFile())   { 
    copyFile(source,   target); 
    System.out.println( "是文件 "); 
    }   else   { 
    System.out.println( "是目录 "); 
    /**  应该是这个语句出现的问题,应该创建目标文件夹而不是源文件夹 */
    target.mkdirs(); 

    File[]   a   =   source.listFiles(); 
    for   (int   i   =   0;   i   <   a.length;   i++)   { 
    copyFolder(a[i],   target); 


    }