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程序设计有所帮助。

相关文章

  • 用java实现的获取优酷等视频缩略图的实现代码

    用java实现的获取优酷等视频缩略图的实现代码

    想获取优酷等视频缩略图,在网上没有找到满意的资料,参考了huangdijia的PHP版工具一些思路,写了下面的JAVA版代码。。其实也可以做成JS版的
    2013-05-05
  • java多线程编程实现下雪效果

    java多线程编程实现下雪效果

    这篇文章主要介绍了java多线程编程实现下雪效果的相关资料,需要的朋友可以参考下
    2015-11-11
  • 新手初学Java基础

    新手初学Java基础

    这篇文章主要介绍了java基础之方法详解,文中有非常详细的代码示例,对正在学习java基础的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-07-07
  • SpringMVC实现数据绑定及表单标签

    SpringMVC实现数据绑定及表单标签

    这篇文章主要为大家详细介绍了SpringMVC实现数据绑定及表单标签的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • PageHelper在springboot中的使用方式

    PageHelper在springboot中的使用方式

    这篇文章主要介绍了PageHelper在springboot中的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • 一篇文章带你入门Java字面量和常量

    一篇文章带你入门Java字面量和常量

    这篇文章主要介绍了探究Java的常量,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • SpringMvc配置静态资源访问路径的实现

    SpringMvc配置静态资源访问路径的实现

    本文主要介绍了SpringMvc配置静态资源访问路径的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • 深入讲解基于JDK的动态代理机制

    深入讲解基于JDK的动态代理机制

    众所周知相比于静态代理,动态代理避免了开发人员编写各个繁锁的静态代理类,下面这篇文章主要给大家介绍了关于基于JDK的动态代理机制的相关资料,文中通过图文以及示例代码介绍的非常详细,需要的朋友可以参考下
    2018-07-07
  • springboot配置templates直接访问的实现

    springboot配置templates直接访问的实现

    这篇文章主要介绍了springboot配置templates直接访问的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • java判断两个时间是不是同一天的方法

    java判断两个时间是不是同一天的方法

    这篇文章主要介绍了java判断两个时间是不是同一天的方法,需要的朋友可以参考下
    2014-02-02

最新评论