java 列出子目录,列出Java中目录和子目录中的所有文件

本文介绍了一种有效处理大量目录及子目录中文件名的方法,通过递归遍历,讨论了可能的瓶颈并指出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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值