列出Java中目录和子目录中的所有文件 [英] list all files from directories and subdirectories in Java

查看:118
本文介绍了列出Java中目录和子目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



编辑列表从1000多个目录和子目录中列出文件名称的最快方法是什么?
我使用的当前代码是:

  import java.io.File; 

public class DirectoryReader {

static int spc_count = -1;

static void Process(File aFile){
spc_count ++;
String spcs =; (int i = 0; i< spc_count; i ++)
spcs + =;
if(aFile.isFile())
System.out.println(spcs +[FILE]+ aFile.getName());
else if(aFile.isDirectory()){
System.out.println(spcs +DIR]+ aFile.getName());
File [] listOfFiles = aFile.listFiles();
if(listOfFiles!= null){
for(int i = 0; i< listOfFiles.length; i ++)
进程(listOfFiles [i]);
} else {
System.out.println(spcs +[ACCESS DENIED]);
}
}
spc_count--;
}

public static void main(String [] args){
String nam =D:/;
文件aFile = new File(nam);
进程(aFile);
}

}


解决方案

这看起来很好(递归地浏览目录)瓶颈将是您需要做的所有文件i / o,优化Java将不会显示任何真正的改进。


What would be the fastest way to list the names of files from 1000+ directories and sub-directories?

EDIT; The current code I use is:

import java.io.File;

public class DirectoryReader {

  static int spc_count=-1;

  static void Process(File aFile) {
    spc_count++;
    String spcs = "";
    for (int i = 0; i < spc_count; i++)
      spcs += " ";
    if(aFile.isFile())
      System.out.println(spcs + "[FILE] " + aFile.getName());
    else if (aFile.isDirectory()) {
      System.out.println(spcs + "[DIR] " + aFile.getName());
      File[] listOfFiles = aFile.listFiles();
      if(listOfFiles!=null) {
        for (int i = 0; i < listOfFiles.length; i++)
          Process(listOfFiles[i]);
      } else {
        System.out.println(spcs + " [ACCESS DENIED]");
      }
    }
    spc_count--;
  }

  public static void main(String[] args) {
    String nam = "D:/";
    File aFile = new File(nam);
    Process(aFile);
  }

}

解决方案

This looks fine (Recursively going through the directory) The bottleneck will be all the file i/o you need to do, optimizing your Java will not show any real improvements.

这篇关于列出Java中目录和子目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆