Java-io Stream (File object-depth traversal of folders and files under the specified directory)

Source: Internet
Author: User

Requirements: traverse this tree structure

   File (String pathname)  ' \ \ ' In order to escape the ' \ '///  through the abstract path pathname create a new document or directory     , "e:\\ Test\\aa "); //       exists determine whether a    file or directory exists; if (! parent.exists ()) {//          mkdirs Create multilevel directory         parent.mkdirs ();    }
ImportJava.io.File; Public classTest { Public Static voidMain (String args[]) {File file=NewFile ("d:\\ #test");//This folder already exists in the computer, do not need to create a newlistall (file); //Traverse this folder (all files and folder names in the directory)    }    Private Static voidlistall (file file) {//gets all file objects and filenames under the specified directory .File[] F =File.listfiles ();  for(File ff:f) {System.out.println (Ff.getabsolutepath ());
System.out.println (Ff.getname ()); } }}

ImportJava.io.File; Public classTest { Public Static voidMain (String args[]) {File file=NewFile ("d:\\ #test");//This folder already exists in the computer, do not need to create a newListall (file,0); //Traverse this folder (all files and folder names in the directory)    }    Private Static voidListall (File file,intLevel ) {System.out.println (Getsapce level)+file.getname ()); //gets all file objects and filenames under the specified directory .File[] F =File.listfiles (); level++;  for(File ff:f) {if(Ff.isdirectory ()) {Listall (ff,level); }ElseSystem.out.println (Getsapce (level)+ff.getname ()); }        }         Public StaticString Getsapce (intLevel ) {StringBuilder SB=NewStringBuilder ();  for(intx=0;x<level;x++) {sb.append ("|----"); }        returnsb.tostring (); }}

Io Stream (File Object-practice-deep Traverse folder)

Java-io Stream (File object-depth traversal of folders and files under the specified directory)

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.