public static void CopyDirecotry(File fromDir, File toDir) throws Exception {
// File f = new File(fromDir.getName());
toDir.mkdir();
if (fromDir.isDirectory()) {
File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
if (Dirname[i].isDirectory()) {
CopyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile()
+ "\\" + Dirname[i].getName()));
} else {
copyFile(Dirname[i], Dirname[i].getName()); }
}
}
}
public static void copyFile(File fromFile, String toFile)
throws IOException {
FileReader from = new FileReader(fromFile);
File ff = new File(toFile);
if (!ff.exists()) {
ff.createNewFile();
}
FileWriter to = new FileWriter(ff);
char[] ch = new char[(int) fromFile.length()];
while (from.read(ch) != -1);
from.close();
to.write(ch);
to.flush();
to.close();
System.out.println("文件成功复制!!!");
}public static void main(String[] args) throws IOException {
try {
File fromDir = new File(getFile + File.separator + "bin");
File toDir = new File(getFile + File.separator + "bin1");
CopyDirecotry(fromDir, toDir);
} catch (Exception e) {
e.printStackTrace();
}
}

解决方案 »

  1.   

    getFile那段代码呢?为什么不贴上来?
      

  2.   

    String getFile = System.getProperty("user.dir");
      

  3.   


    import java.io.*;
    public class TestCopyFile {
        public static void copyDirecotry(File fromDir, File toDir) throws Exception {
    // File f = new File(fromDir.getName());
    toDir.mkdir();
    if (fromDir.isDirectory()) {
        File[] Dirname = fromDir.listFiles();     for (int i = 0; i < Dirname.length; i++) {
    if (Dirname[i].isDirectory()) {
        copyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile()
        + "\\" + Dirname[i].getName()));
    } else {
        copyFile(Dirname[i], Dirname[i].getName());
    }
        }
    }
        }    public static void copyFile(File fromFile, String toFile)
        throws IOException {
    FileReader from = new FileReader(fromFile);
    File ff = new File(toFile);
    if (!ff.exists()) {
        ff.createNewFile();
    }
    FileWriter to = new FileWriter(ff);
    char[] ch = new char[(int) fromFile.length()];
    while (from.read(ch) != -1)
        ;
    from.close();
    to.write(ch);
    to.flush();
    to.close();
    //System.out.println("文件成功复制!!!");
        }    public static void main(String[] args) throws Exception {
    String getFile = System.getProperty("user.dir");
     File fromDir = new File(getFile + File.separator + "bin");
     File toDir = new File(getFile + File.separator + "bin1");
    if(toDir!=null) System.out.println(toDir);//D:\Program Files\MyEclipseProject\Exercise\bin1
    if(!toDir.isDirectory())  toDir.mkdirs();    //记得要写这一句
    copyDirecotry(fromDir, toDir);//函数名首字母小写

        }}
      

  4.   

    import java.io.*;public class TestCopyFile {
        public static void copyDirecotry(String fromDirName, String toDirName)
        throws Exception { File fromDir = new File(fromDirName);
    if (!fromDir.isDirectory())
        return;
    File toDir = new File(toDirName);
    if (!toDir.isDirectory())
        toDir.mkdirs(); // 记得要写这一句
    File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
        if (Dirname[i].isDirectory()) {
    copyDirecotry(Dirname[i].getAbsolutePath(), toDir
    .getAbsoluteFile()
    + "\\" + Dirname[i].getName());
        } else {
    copyFile(Dirname[i].getAbsolutePath(), toDir.getAbsoluteFile()
    + "\\" + Dirname[i].getName());
        }
    }    }    public static void copyFile(String fromFileName, String toFileName)
        throws IOException { BufferedReader br = new BufferedReader(new FileReader(fromFileName));
    BufferedWriter bw = new BufferedWriter(new FileWriter(toFileName)); String s;
    StringBuffer sb = new StringBuffer();
    while ((s = br.readLine()) != null) {
        bw.write(s);
        bw.newLine();
        bw.flush();
    }
    bw.close();
    br.close();
    // System.out.println("文件成功复制!!!");
        }    public static void main(String[] args) throws Exception {
    String getFile = System.getProperty("user.dir");
    File fromDir = new File(getFile + File.separator + "bin");
    File toDir = new File(getFile + File.separator + "bin1");
    // if(toDir!=null) System.out.println(toDir);//D:\Program
    // Files\MyEclipseProject\Exercise\bin1
    copyDirecotry(fromDir.getAbsolutePath(),
    toDir.getAbsolutePath());//函数名首字母小写
        }}
      

  5.   

    还有就是,在复制bin目录时,可能有程序在使用bin目录下的文件,应该是冲突吧,最后复制出来的bin1目录和
    原先的bin目录大小不一样,我的机器是bin1要大于bin
    你只要把bin和bin1改为其他目录,就能复制相同大小的目录,比如src 和 src1,
      

  6.   

    你的代码在目录和文件的复制功能上已经实现了,没有问题,之所以你没有看到目录中文件复制过去,是因为在复制文件的时候,目标位置的路径传错了:
    你的代码运行的话:复制的文件都在bin那个目录的父目录下要实现对应目录的文件复制,只需要改一下这个地方的代码就可以:copyFile(Dirname[i], toDir.getCanonicalPath()+File.separator+Dirname[i].getName());
      

  7.   

    copyFile(Dirname[i], toDir.getCanonicalPath()+File.separator+Dirname[i].getName());
      

  8.   

    不行?
    不可能啊,我把你的代码直接COPY过去的,然后只把copyFile方法传的参数改了一下,就好了:
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;public class CopyDirecotries { public static void CopyDirecotry(File fromDir, File toDir) throws Exception {
    // File f = new File(fromDir.getName());
    toDir.mkdir();
    if (fromDir.isDirectory()) {
    File[] Dirname = fromDir.listFiles(); for (int i = 0; i < Dirname.length; i++) {
    if (Dirname[i].isDirectory()) {
    CopyDirecotry(Dirname[i], new File(toDir.getAbsoluteFile() + "\\" + Dirname[i].getName()));
    } else {
    copyFile(Dirname[i], toDir.getCanonicalPath()+File.separator+Dirname[i].getName());//此处代码就行了修改 }
    }
    }
    } public static void copyFile(File fromFile, String toFile) throws IOException {
    FileReader from = new FileReader(fromFile);
    File ff = new File(toFile);
    if (!ff.exists()) {
    ff.createNewFile();
    }
    FileWriter to = new FileWriter(ff);
    char[] ch = new char[(int) fromFile.length()];
    while (from.read(ch) != -1)
    ;
    from.close();
    to.write(ch);
    to.flush();
    to.close();
    System.out.println("文件成功复制!!!");
    } /**
     * @param args
     */
    public static void main(String[] args) {
    String getFile = System.getProperty("user.dir");
    try {
    System.out.println("filepath: " + getFile + File.separator + "bin");
    File fromDir = new File(getFile + File.separator + "bin");
    File toDir = new File(getFile + File.separator + "bin1"); if (fromDir.exists() && toDir.exists()) {
    System.out.println("YES");
    } CopyDirecotry(fromDir, toDir); } catch (Exception e) {
    e.printStackTrace();
    } }}