Traverse the files in the folder (How far can we go Series 2)

Source: Internet
Author: User
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 ;} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.