Java displays the names of specific folders and subfolders in the command line.

Source: Internet
Author: User

It is easier to display the names of specific folders, files, subfolders, and subfolders in the command line using recursive calling (calling the parent method). The following is an example:Code:

Import Java. io. *; public class filelist {public static void main (string [] ARGs) {file F = new file ("D:/a"); system. out. println (F. getname (); // obtain the name tree (F, 1) of the F folder;} Private Static void tree (file F, int level) {string prestr = ""; for (INT I = 0; I <level; I ++) // insert Space {prestr + = "";} file [] Childs = f. listfiles (); // obtain the subfolders and subfolders of the f folder, and return the array for (INT I = 0; I <Childs. length; I ++) {system. out. println (prestr + Childs [I]. getname (); // name of the Output Folder if (Childs [I]. isdirectory () {tree (Childs [I], level + 1); // call the parent method }}}}

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.