大家复制一下,运行一下,肯定会让你觉得很奇怪。
问题:
allFileName 数组持有文件夹下的文件夹或文件名,所以我想把它单独拿出来,但是这样做了以后,却会发现无论你怎么调试,将数组拿出来以后,所要复制的文件一定会少!!所以请大家复制后运行下看看。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;public class Test {

static String[] allFileName; public static void test(String oldPath, String newPath){
String[] allName = new File(oldPath).list();
allFileName = allName;
copyFolder(oldPath, newPath);
}

public static void main(String[] args){
//test("D:\\333", "C:\\333");
copyFolder("D:\\333", "C:\\333");  //原D:\\333里有文件夹 222 其中有2个文件,但是通过上面的方式运行这只能得到1个文件,非常非常奇怪~~!!
}

public static File copyFolder(final String oldPath, final String newPath) { File newFile = new File(newPath);
try {
newFile.mkdir();
File oldFolder = new File(oldPath);  //将这句话注释掉
String[] allFileName = oldFolder.list(); //将这句话注释掉 File temp = null; for (int i = 0; i < allFileName.length; i++) {
if (oldPath.endsWith(System.getProperty("file.separator"))) {
temp = new File(oldPath + allFileName[i]);
} else {
temp = new File(oldPath
+ System.getProperty("file.separator")
+ allFileName[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath
+ System.getProperty("file.separator")
+ (temp.getName()).toString()); BufferedReader inputReader = new BufferedReader(
new InputStreamReader(input));
BufferedWriter outputWriter = new BufferedWriter(
new OutputStreamWriter(output)); char[] data = new char[1024 * 5]; int len = 0; while ((len = inputReader.read(data)) != -1) {
outputWriter.write(data, 0, len);
}
output.flush();
inputReader.close();
outputWriter.close();
}
if (temp.isDirectory()) {
copyFolder(oldPath + System.getProperty("file.separator")
+ allFileName[i], newPath
+ System.getProperty("file.separator")
+ allFileName[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("复制出现错误!");
}
return newFile;
}
}

解决方案 »

  1.   

    注释上说了,把copyFolder方法中的两句注释掉,然后调用test方法~~
      

  2.   

    注释掉你说的之后调用test有问题
    因为你是递归调用copyFolder,但全局变量没有改变,导致后面的文件找不到
    运行下面的代码你就能发现问题了package com.train.first;import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;public class Test
    { static String[] allFileName; public static void test(String oldPath, String newPath)
    {
    String[] allName = new File(oldPath).list();
    allFileName = allName;
    copyFolder(oldPath, newPath);
    } public static void main(String[] args)
    {
    test("D:\\333", "C:\\333");
    //copyFolder("D:\\333", "C:\\333"); //原D:\\333里有文件夹 222 其中有2个文件,但是通过上面的方式运行这只能得到1个文件,非常非常奇怪~~!!
    } public static File copyFolder(final String oldPath, final String newPath)
    {
    File newFile = new File(newPath);
    try
    {
    newFile.mkdir();
    //File oldFolder = new File(oldPath); //将这句话注释掉
    //String[] allFileName = oldFolder.list(); //将这句话注释掉 File temp = null; for (int i = 0; i < allFileName.length; i++)
    {
    if (oldPath.endsWith(System.getProperty("file.separator")))
    {
    temp = new File(oldPath + allFileName[i]);
    }
    else
    {
    temp = new File(oldPath
    + System.getProperty("file.separator")
    + allFileName[i]);
    }

    System.out.println(temp);

    if (temp.isFile())
    {
    FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath
    + System.getProperty("file.separator")
    + (temp.getName()).toString()); BufferedReader inputReader = new BufferedReader(
    new InputStreamReader(input));
    BufferedWriter outputWriter = new BufferedWriter(
    new OutputStreamWriter(output)); char[] data = new char[1024 * 5]; int len = 0; while ((len = inputReader.read(data)) != -1)
    {
    outputWriter.write(data, 0, len);
    }
    output.flush();
    inputReader.close();
    outputWriter.close();
    }
    if (temp.isDirectory())
    {
    copyFolder(oldPath + System.getProperty("file.separator")
    + allFileName[i], newPath
    + System.getProperty("file.separator")
    + allFileName[i]);
    }
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    System.out.println("复制出现错误!");
    }
    return newFile;
    }
    }