java - ifFile 不识别 csv、pl、sh、xml 文件
问题描述
我正在构建一个目录的递归搜索数组。这部分效果很好,但是在尝试确定文件是否为文件时,它只会将 .txt 文件添加到数组中,并跳过 csv、pl、sh、xml 等文件。我能做些什么来解决这个问题吗?这是我正在使用的代码。
public static ArrayList<Object> listDirectory(String directory) {
Object sbytes;
File dir = new File((String) directory);
File[] firstLevelFiles = dir.listFiles();
ArrayList<Object> array = new ArrayList <Object>();
if (firstLevelFiles != null) {
for (File aFile : firstLevelFiles) {
//if (aFile.isFile()) {
if (! aFile.isDirectory()) {
System.out.println("[" + aFile.getAbsolutePath() + "]");
long bytes = aFile.length();
if (bytes > 1000) {
sbytes = bytes / 1000 + " Kb";
} else if (bytes > 1000000){
sbytes = bytes / 1000000 + " Mb";
} else {
sbytes = bytes + " bytes";
}
Object fileName = aFile.getName();
Object nameAndSize = fileName + " " + sbytes;
array.add(nameAndSize);
} else {
ArrayList<Object> deeperList = listDirectory(aFile.getAbsolutePath());
array.addAll(deeperList);
}
}
}
return array;
}
解决方案
推荐阅读
- dart - Dart 中的随机二叉树
- javascript - vue.js 箭头键绑定问题
- reactjs - React 允许某些 url 的服务器端渲染
- assembly - 这些代码行在 x86 汇编中意味着什么?
- javascript - 使用 Javascript 和 Canvas 重复绘图
- django - 无效的 HTTP_HOST 标头:提供的域名无效——使用容器名称向 dockerized django 应用程序请求
- .net - 如何使用 ImageResizer 插件添加 Access-Control-Allow-Origin 标头
- azure - Power bi 服务在 Azure 中不可用
- r - 将文件从 Sharepoint 导入到 R
- jquery - Jquery 日期选择器没有在文本字段的焦点上触发