java實(shí)現(xiàn)遞歸文件列表的方法
本文實(shí)例講述了java實(shí)現(xiàn)遞歸文件列表的方法。分享給大家供大家參考。具體如下:
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); } } }
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
- java、js中實(shí)現(xiàn)無(wú)限層級(jí)的樹(shù)形結(jié)構(gòu)方法(類(lèi)似遞歸)
- Java之遞歸求和的兩種簡(jiǎn)單方法(推薦)
- Java遞歸方法求5!的實(shí)現(xiàn)代碼
- 利用java+mysql遞歸實(shí)現(xiàn)拼接樹(shù)形JSON列表的方法示例
- java遞歸讀取目錄下所有文件的方法
- Java編程獲取文件列表及子文件目錄的方法(非遞歸)
- 遞歸之斐波那契數(shù)列java的3種方法
- 全排列算法-遞歸與字典序的實(shí)現(xiàn)方法(Java)
- java數(shù)學(xué)歸納法非遞歸求斐波那契數(shù)列的方法
- java中方法遞歸的簡(jiǎn)單示例
相關(guān)文章
Java多線程之readwritelock讀寫(xiě)分離的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java多線程之readwritelock讀寫(xiě)分離的相關(guān)內(nèi)容,文中涉及具體實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10使用IDEA開(kāi)發(fā)配置Java Web的初始化過(guò)程
該教程使用idea開(kāi)發(fā)工具初始化javaweb項(xiàng)目,該運(yùn)行在tomcat服務(wù)器上通過(guò)配置項(xiàng)目環(huán)境變量保證tomcat正常啟動(dòng),具體操作配置教程參考下本文2021-06-06解決在Idea 2020.2下使用 Lombok的注解不生效的問(wèn)題(插件安裝了,依賴(lài)也寫(xiě)了,自動(dòng)注解也設(shè)置了)
這篇文章主要介紹了在Idea 2020.2下使用 Lombok的注解不生效的問(wèn)題(插件安裝了,依賴(lài)也寫(xiě)了,自動(dòng)注解也設(shè)置了),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08仿釘釘流程輕松實(shí)現(xiàn)JSON轉(zhuǎn)BPMN完整實(shí)現(xiàn)過(guò)程示例
這篇文章主要為大家介紹了仿釘釘流程輕松實(shí)現(xiàn)JSON轉(zhuǎn)BPMN完整實(shí)現(xiàn)過(guò)程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08@Schedule?如何解決定時(shí)任務(wù)推遲執(zhí)行
這篇文章主要介紹了@Schedule?如何解決定時(shí)任務(wù)推遲執(zhí)行問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Java數(shù)據(jù)結(jié)構(gòu)之堆(優(yōu)先隊(duì)列)詳解
堆(優(yōu)先隊(duì)列)是一種典型的數(shù)據(jù)結(jié)構(gòu),其形狀是一棵完全二叉樹(shù),一般用于求解topk問(wèn)題。本文將利用Java語(yǔ)言實(shí)現(xiàn)堆,感興趣的可以學(xué)習(xí)一下2022-07-07