How far can we go series (2)
Traverse the files in the specified path. The files and folders must be differentiated.
To record the file name and whether it is a folder, implement a model
Package Web. method. file. model; Public Class Filemodel { // Is it a folder? Private Boolean Isdirectory; // File Name Private String filenmae; Public Filemodel ( Boolean Isdirectory, string filenmae ){ Super (); This . Isdirectory = Isdirectory; This . Filenmae = Filenmae ;} Public Boolean Isdirectory (){ Return Isdirectory ;} Public Void Setdirectory ( Boolean Isdirectory ){ This . Isdirectory = Isdirectory ;} Public String getfilenmae (){ Return Filenmae ;} Public Void Setfilenmae (string filenmae ){ This . Filenmae =Filenmae ;}}
Traversing folders Java encapsulates the methods for use:
/** ** @ Param String * path Query file path ** @ Return Map <Boolean, string> Boolean-> true: folder; false: Non-folder, string: file name/folder name */ Private List <filemodel> Queryallfilename (string path ){ // Ensure that the path ends with "/" or "\" If ((! Path. endswith (file. pathseparator) | (! Path. endswith ("\\" ) {Path = Path + File. pathseparator ;} // Query path File filepath = New File (PATH ); // Path does not exist If (! Filepath. exists ()){ Return Null ;} List <Filemodel> filemodellist = New Arraylist <filemodel> (); // PATH is not a folder If (! Filepath. isdirectory () {filemodel File = New Filemodel ( False , PATH); filemodellist. Add (File ); Return Filemodellist ;} // Get the path name or folder name String [] filenames = Filepath. List (); For ( Int I = 0; I <filenames. length; I ++ ){ // Determine whether a folder is used If (( New File (path + Filenames [I]). isdirectory () {filemodellist. Add ( New Filemodel ( True , Filenames [I]);} Else {Filemodellist. Add ( New Filemodel ( False , Filenames [I]);} Return Filemodellist ;}