如何知道递归文件扫描的当前文件夹深度



我编写了一个函数,该函数递归地扫描文件夹以搜索文件并对其进行处理(foundFile())。

问题是当函数找到别名文件夹时(例如在mac中,当我试图在/Volumes文件夹中循环时)。脚本无限循环(我不知道为什么)。是否有可能知道递归的当前"深度"并在(即)20处停止?

或者甚至在特定情况下停止循环(别名文件夹)

private String[] types = { ".wav", ".mp3", ".ogg", ".wave", ".wma"};
public void listFile(String pathname) {
    File f = new File(pathname);
    File[] listfiles = f.listFiles();
    if(listfiles!=null){
        for (int i = 0; i < listfiles.length; i++) {
            if (listfiles[i].isDirectory()) {
                File[] internalFile = listfiles[i].listFiles();
                if(internalFile!=null){
                    for (int j = 0; j < internalFile.length; j++) {
                        for(int h=0;h<types.length;h++){
                            if(internalFile[j].getAbsolutePath().endsWith(types[h])){
                                found.put(types[h], found.get(types[h])+1);
                                foundFile(internalFile[j]);
                            }
                        }
                        if (internalFile[j].isDirectory()) {
                            String name = internalFile[j].getAbsolutePath();
                            listFile(name);
                        }
                    }
                }
            } else {
                processed+=1;
                for(int j=0;j<types.length;j++){
                    if(f.getAbsolutePath().endsWith(types[j])){
                        this.found.put(types[j], found.get(types[j])+1);
                        foundFile(listfiles[i]);
                    }
                }
            }
        }
    }
}

类似这样的东西,添加一个级别参数:

public void listFile(final String pathname, int level) {
    if (level == 20){
        return;
    }
    final File f = new File(pathname);
    final File[] listfiles = f.listFiles();
    if (listfiles != null) {
        for (int i = 0; i < listfiles.length; i++) {
            if (listfiles[i].isDirectory()) {
                final File[] internalFile = listfiles[i].listFiles();
                if (internalFile != null) {
                    for (int j = 0; j < internalFile.length; j++) {
                        for (int h = 0; h < types.length; h++) {
                            if (internalFile[j].getAbsolutePath().endsWith(types[h])) {
                                found.put(types[h], found.get(types[h]) + 1);
                                foundFile(internalFile[j]);
                            }
                        }
                        if (internalFile[j].isDirectory()) {
                            final String name = internalFile[j].getAbsolutePath();
                            listFile(name, level + 1);
                        }
                    }
                }
            } else {
                processed += 1;
                for (int j = 0; j < types.length; j++) {
                    if (f.getAbsolutePath().endsWith(types[j])) {
                        this.found.put(types[j], found.get(types[j]) + 1);
                        foundFile(listfiles[i]);
                    }
                }
            }
        }
    }
}

最新更新