java实现递归文件列表的方法
作者:华宰 时间:2022-10-13 13:17:00
本文实例讲述了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程序设计有所帮助。
标签:java,递归,文件
0
投稿
猜你喜欢
Unity5.6大规模地形资源创建方法
2022-07-12 20:59:55
Android实现短信验证码自动填写功能
2022-01-21 13:12:10
Unity3d实现跑马灯广播效果
2022-11-13 20:08:12
浅谈Maven镜像更换为阿里云中央仓库(精)
2022-08-06 04:48:17
Java的动态分派和静态分派的实现
2023-10-09 12:58:37
Go Java算法之从英文中重建数字示例详解
2023-10-25 02:37:26
C#泛型与非泛型性能比较的实例
2022-01-31 17:26:51
Android PhoneWindowManager监听屏幕右侧向左滑动实现返回功能
2023-02-09 17:50:46
一文了解自定义MVC框架实现
2023-01-11 00:15:10
C#实现String类型和json之间的相互转换功能示例
2023-06-18 07:35:32
C#递归实现显示文件夹及所有文件并计算其大小的方法
2023-09-13 01:56:01
Spring Boot 结合 aop 实现读写分离
2023-09-29 07:53:02
Gradle的基本使用
2023-05-29 00:08:42
Java通俗易懂系列设计模式之建造者模式
2022-11-01 23:27:24
Java之Jackson的基本使用案例讲解
2022-05-27 22:08:49
SSh结合Easyui实现Datagrid的分页显示
2022-01-14 21:46:49
Android实战教程第七篇之如何在内存中存储用户名和密码
2021-07-15 15:43:14
java中复杂查询sql语句该怎么写
2021-06-20 01:25:24
C# web.config之<customErrors>节点说明案例详解
2023-07-06 20:20:15
Android EditText自定义样式的方法
2021-10-06 22:37:37