java实现递归文件列表的方法

 更新时间:2015年07月21日 17:06:26   作者:华宰  
这篇文章主要介绍了java实现递归文件列表的方法,实例分析了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程序设计有所帮助。

相关文章

  • SpringBoot读取配置的6种方式

    SpringBoot读取配置的6种方式

    本文主要介绍了SpringBoot读取配置的6种方式,主要包括使用默认配置、使用application.properties文件、使用application.yml文件、使用@Value注解、使用Environment对象和使用ConfigurableEnvironment对象,感兴趣的可以了解一下
    2023-08-08
  • Java多线程之readwritelock读写分离的实现代码

    Java多线程之readwritelock读写分离的实现代码

    这篇文章主要介绍了Java多线程之readwritelock读写分离的相关内容,文中涉及具体实例代码,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • 使用IDEA开发配置Java Web的初始化过程

    使用IDEA开发配置Java Web的初始化过程

    该教程使用idea开发工具初始化javaweb项目,该运行在tomcat服务器上通过配置项目环境变量保证tomcat正常启动,具体操作配置教程参考下本文
    2021-06-06
  • Java线程同步实例分析

    Java线程同步实例分析

    这篇文章主要介绍了Java线程同步用法,实例分析了java中线程同步的相关实现技巧与注意事项,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 解决在Idea 2020.2下使用 Lombok的注解不生效的问题(插件安装了,依赖也写了,自动注解也设置了)

    解决在Idea 2020.2下使用 Lombok的注解不生效的问题(插件安装了,依赖也写了,自动注解也设置了)

    这篇文章主要介绍了在Idea 2020.2下使用 Lombok的注解不生效的问题(插件安装了,依赖也写了,自动注解也设置了),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Java Home变量的详细配置操作步骤

    Java Home变量的详细配置操作步骤

    用到Java项目的时候,有时候要用到Java_home,这个需要在系统配置中配置一下,如何操作呢?以下为详细的图文步骤,感兴趣的朋友跟随小编一起看看吧
    2023-11-11
  • EasyCode插件使用详解(推荐)

    EasyCode插件使用详解(推荐)

    EasyCode是idea的一个插件,这个插件功能很强大,今天通过本文给大家分享EasyCode插件使用详解,需要的朋友可以参考下
    2020-09-09
  • 仿钉钉流程轻松实现JSON转BPMN完整实现过程示例

    仿钉钉流程轻松实现JSON转BPMN完整实现过程示例

    这篇文章主要为大家介绍了仿钉钉流程轻松实现JSON转BPMN完整实现过程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • @Schedule 如何解决定时任务推迟执行

    @Schedule 如何解决定时任务推迟执行

    这篇文章主要介绍了@Schedule 如何解决定时任务推迟执行问题。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Java数据结构之堆(优先队列)详解

    Java数据结构之堆(优先队列)详解

    堆(优先队列)是一种典型的数据结构,其形状是一棵完全二叉树,一般用于求解topk问题。本文将利用Java语言实现堆,感兴趣的可以学习一下
    2022-07-07

最新评论