Original: Share very useful Java programs (key code) (ii)---list files and directories
File dir = new file ("DirectoryName"); String[] Children = dir.list (); if (children = = null) {//either Dir does not exist or was not a directory} else {for (int i=0; I &l T Children.length; i++) {//Get filename of file or directory String filename = children[i]; }}//It is also possible to filter the list of returned files. This example does no return any files that start with '. '. FilenameFilter filter = new FilenameFilter () {public Boolean accept (File dir, String name) {return! Name.startswith ("."); } }; Children = dir.list (filter); The list of files can also be retrieved as File objects file[] files = dir.listfiles (); This filter is returns directories FileFilter FileFilter = new FileFilter () {public Boolean accept (File File) {return file.isdirectory (); } }; Files = Dir.listfiles (filefilter);
--hurry
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Share very useful Java programs (key code) (ii)---list files and directories