Example of recursively traversing folders

Source: Internet
Author: User

Used in the project, find all msml files in a folder, use string to return their absolute paths, and put them in a list. There may be folders in the folder, and msml files may be placed in any level-1 folder. Here we actually write a basic recursive folder to find a method that meets the requirements.

Package meta. util; import Java. io. file; import Java. util. arraylist; public class scandir {private arraylist <string> filelist = new arraylist <string> (); // put it in a list public arraylist <string> getfilelist () {return filelist ;} public void setfilelist (arraylist <string> filelist) {This. filelist = filelist;} // recursively searches for all msml files under the strpath with the public void refreshfilelist (string strpath) {string filename; // file name string SUF ;/ /File suffix file dir = new file (strpath); // folder dirfile [] files = dir. listfiles (); // all files in the folder or folders if (Files = NULL) return; For (INT I = 0; I <files. length; I ++) {If (files [I]. isdirectory () {system. out. println ("---" + files [I]. getabsolutepath (); refreshfilelist (files [I]. getabsolutepath (); // recursive folder !!!} Else {filename = files [I]. getname (); Int J = filename. lastindexof (". "); SUF = filename. substring (J + 1); // obtain the file suffix if (SUF. equalsignorecase ("msml") // determine whether the file is a msml suffix {string strfilename = files [I]. getabsolutepath (). tolowercase (); filelist. add (files [I]. getabsolutepath (); // Add the path of the file to filelist before the file is created }}}}}

After such a list is obtained, each msml file can be parsed in sequence, and Dom or sax can be used to obtain metadata information.

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.