国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現遞歸文件列表的方法

2019-11-26 15:04:50
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河南省| 濉溪县| 区。| 邯郸市| 景东| 聂荣县| 梁平县| 讷河市| 灵川县| 江陵县| 布拖县| 乡宁县| 丹阳市| 外汇| 九龙城区| 遂平县| 五家渠市| 和平区| 得荣县| 桦南县| 西藏| 淮南市| 彝良县| 彩票| 辽源市| 惠东县| 五家渠市| 河津市| 武宁县| 白水县| 民勤县| 西宁市| 兴文县| 江川县| 犍为县| 荆门市| 陵川县| 孟津县| 太原市| 都江堰市| 建湖县|