为什么我这个在递归输出的时候打印不出下面的子目录的文件阿?比如 D:\java\code\com\neusoft\
下面有12个文件这个程序打印不出来它的名字?这是为什么?import java.io.*;
public class TestFile
{
public static void main(String [] args) {
TestFile r = new TestFile();
r.s("D:\\java\\code\\com\\");
}
private void s(String k) {
File in = new File(k);
String [] arr; if(in.exists()) {
if(in.isDirectory()) {
System.out.println("1");
arr = in.list();
int i = 0;
while(i < arr.length)
{
s(arr[i]);
i++;
}
}
else {
System.out.println("File name : "+in.getName());
}
}
else {
System.out.println("No have file!");
}
}
}

解决方案 »

  1.   

    建议String[] arr 改为File[] files
    然后 files = in.listFiles();private File[] getObjFile() {
    String objFilePath = this.getApplicationPath();
    objFilePath += this.relativeObjFileColumn;
    File file = new File(objFilePath);
    File[] objFiles = null;
    if(file.isDirectory()) {
    objFiles = file.listFiles();
    }
    return objFiles;
    }
    public void bakFile() {
    File[] file = this.getObjFile();
    for(int i=0; i<file.length; i++) {
    this.bakFile(file[i]);
    }
    }参考一下
      

  2.   


    package test;//递归打印出树形目录
    import java.io.*;public class FileList {
    public static void main(String[] args)throws Exception {
    File f = new File("D:/soft");
    System.out.println(f.getName());

    //FileWriter fw = null;

    tree(f, 1);
    }

    private static void tree(File f, int level) {
    FileReader fr  = null;
    BufferedReader br = null;
    int c = 0;
    String preStr = "";
    for(int i=0; i<level; i++) {
    preStr += "    ";
    }

    File[] childs = f.listFiles();
    for(int i=0; i<childs.length; i++) {
    System.out.println(preStr + childs[i].getName());
    if(childs[i].isDirectory()) {
    tree(childs[i], level + 1);
    }else {
    System.out.println(childs[i].getName());
    /*try{
     fr=new FileReader(childs[i]);
     br= new BufferedReader(fr);
    while((c=br.read()) != -1){
    System.out.print((char)c);
    }
    System.out.println();
    br.close();
    fr.close();
    }
    catch (FileNotFoundException e){
    System.out.println("FileNotFound");
    }
    catch (IOException e){
    System.out.println("文件读取错误");
    }*/
    }
    }
    }
    }
      

  3.   

    主要问题在 arr = in.list();上。File list()方法:如果此抽象路径名不表示一个目录,那么此方法将返回 null。否则返回一个字符串数组,每个数组元素对应目录中的每个文件或目录。表示目录本身及其父目录的名称不包括在结果中。每个字符串是一个文件名,而不是一条完整路径。 所以arr里面全是目录或文件名,而不是完整路径。因此递归之后in.exists()将不会存在的。用listFiles()方法吧,很常用。 
      

  4.   


    package test;import java.io.*;
    public class TestFile
    {
        public static void main(String [] args) {
            TestFile r = new TestFile();
            r.s(new File("D:\\java\\code\\com\\"));
        }
        private void s(File in) {
            
            File[] arr;        if(in.exists()) {
                if(in.isDirectory()) {
                    System.out.println(in.getName());
                    arr = in.listFiles();
                    int i = 0;
                    while(i < arr.length)
                    {
                        s(arr[i]);
                        i++;
                    }
                }
                else {
                    System.out.println("File name : "+in.getName());
                }
            }
            else {
                System.out.println("No have file!");
            }
        }
    }
      

  5.   


    给你改过的在四楼
    更好的例子在二楼
     System.out.println(childs[i].getName());这一行,注释上面那行删掉,或者整个else删掉,里面是读取文件内容的。