国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 革吉县| 衡南县| 巨鹿县| 宁陵县| 徐水县| 承德县| 天全县| 渑池县| 汪清县| 阳江市| 玉门市| 广安市| 剑河县| 杂多县| 枣强县| 宝坻区| 右玉县| 渭南市| 英超| 凤翔县| 德格县| 雷州市| 三河市| 麻城市| 新闻| 池州市| 且末县| 南宁市| 邵东县| 吕梁市| 龙江县| 金门县| 清涧县| 桂平市| 绥中县| 唐山市| 将乐县| 岳普湖县| 永兴县| 高雄县| 乌什县|