java实现递归文件列表的方法
作者:华宰 发布时间:2022-10-13 13:17:00
标签:java,递归,文件
本文实例讲述了java实现递归文件列表的方法。分享给大家供大家参考。具体如下:
FileListing.java如下:
import java.util.*;
import java.io.*;
/**
* Recursive file listing under a specified directory.
*
* @author javapractices.com
* @author Alex Wong
* @author anonymous user
*/
public final class FileListing {
/**
* Demonstrate use.
*
* @param aArgs - <tt>aArgs[0]</tt> is the full name of an existing
* directory that can be read.
*/
public static void main(String... aArgs) throws FileNotFoundException {
File startingDirectory= new File(aArgs[0]);
List<File> files = FileListing.getFileListing(startingDirectory);
//print out all file names, in the the order of File.compareTo()
for(File file : files ){
System.out.println(file);
}
}
/**
* Recursively walk a directory tree and return a List of all
* Files found; the List is sorted using File.compareTo().
*
* @param aStartingDir is a valid directory, which can be read.
*/
static public List<File> getFileListing(
File aStartingDir
) throws FileNotFoundException {
validateDirectory(aStartingDir);
List<File> result = getFileListingNoSort(aStartingDir);
Collections.sort(result);
return result;
}
// PRIVATE //
static private List<File> getFileListingNoSort(
File aStartingDir
) throws FileNotFoundException {
List<File> result = new ArrayList<File>();
File[] filesAndDirs = aStartingDir.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs) {
result.add(file); //always add, even if directory
if ( ! file.isFile() ) {
//must be a directory
//recursive call!
List<File> deeperList = getFileListingNoSort(file);
result.addAll(deeperList);
}
}
return result;
}
/**
* Directory is valid if it exists, does not represent a file, and can be read.
*/
static private void validateDirectory (
File aDirectory
) throws FileNotFoundException {
if (aDirectory == null) {
throw new IllegalArgumentException("Directory should not be null.");
}
if (!aDirectory.exists()) {
throw new FileNotFoundException("Directory does not exist: " + aDirectory);
}
if (!aDirectory.isDirectory()) {
throw new IllegalArgumentException("Is not a directory: " + aDirectory);
}
if (!aDirectory.canRead()) {
throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
}
}
}
希望本文所述对大家的java程序设计有所帮助。


猜你喜欢
- 1.简介本教程将介绍如何在Spring Security中设置身份验证提供程序,与使用简单UserDetailsService的标准方案相比
- 一、概述 这一章先来点有意思的百度地图应用示例,然后再分章详细介绍用C#开发Android App的各种基本技术。 本章
- 本文讲述了Java开发人员需知的十大戒律。分享给大家供大家参考,具体如下:作为一个Java开发人员提高自己代码的质量,可维护性,是个恒久不变
- 1、产生原因其实显示黑屏或者白屏实属正常,这是因为还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。示
- 今天看到已经更新了devblogs,新增的C# 11的!!(用于检查null的语法)经过非常长的讨论,最后取消了。然后我又想起来null检查
- 使用@Provider注意事项(要点)1.在Mapper接口和@InsertProvider方法类中,不要使用重载,也就是说,不要使用方法名
- 设计算法,计算两给定基因序列的相似程度。人类基因由4种核苷酸,分别用字母ACTG表示。要求编写一个程序,按以下规则比较两个基因序列并确定它们
- 配置java环境变量这里是将环境变量配置在etc/profile,即为所有用户配置JDK环境。sudo vi /etc/profile配置环
- 在有向图中,边是单向的:每条边连接的两个顶点都是一个有序对,它们的邻接性是单向的。许多应用都是天然的有向图,如下图。为实现添加这种单向性的限
- 逻辑描述:现在我们想在B层和D层加上接口层,并使用工厂。而我们可以将创建B和创建D看作是两个系列,然后就可以使用抽象工厂进行创建了。配置文件
- maven的配置文件settings.xml存在于两个地方:1.安装的地方:${M2_HOME}/conf/settings.xml2.用户
- 不啰嗦,上菜 QueryWrapper queryWrapper = new QueryWrapper(); queryWrapper.se
- 1.什么是逆向工程mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的
- 需求:有些时候,我们需要连接多个数据库,但是,在方法调用前并不知道到底是调用哪个。即同时保持多个数据库的连接,在方法中根据传入的参数来确定。
-   考虑到直接讲实现一个类Task库思维有点跳跃,所以本节主要讲解Async/Await的本质作用(解决
- Android 的APP 需要集成一个蓝牙扫码器, 特别的是,需要扫码的地方是没有输入框的(EditText),不能通过直觉上理解的通过对E
- 目录一,功能二,工具三、效果图:四、数据库设计五、JAVA层次分析六、主要Java代码分析一,功能管理员登录图书借阅信息管理图书信息管理管理
- Surface的拍照实现也是很简单,一个小demo就可以把流程看懂了。 话不多说,直接上代码布局文件<SurfaceView &nbs
- 日常使用中spring的 @Cacheable 大家一定不陌生,基于aop机制的缓存实现,并且可以选择cacheManager具体提供缓存的
- 有时我们可能会遇到下图这样一种情况 — 我们需要的资料或教程被分成了几部分存放在多个PDF文件中,不管是阅读还是保存都不是很方便,这时我们肯