Recursive function, print the folder and file name under the specified file directory, and print out the ___ function under each subfolder.

Source: Internet
Author: User
Import java.io.*; /** given a file path, print the following: If the path points to a folder, print all subfolders and files under that folder (including folders and files under subfolders) if the path points to a file, print the file name if the path points to a nonexistent file, print: "(No FIL E) "* method to create ideas: 1. Create a method in a class: * First, get the file abstract path * Second, to determine whether the path is accurate, otherwise print NO file * Third, the path is correct, judge whether it is a file, if the file, then print file name * Four, the path is not The file name determines whether it is the folder name and, if it is a folder, calls the recursive method to print all folders and files in the folder/public class listfilerecursion{public static void ListFile (String file
	        Path) {try{//1. The abstract path to the file is filename = new file (FilePath); 2. Determine if the path is accurate if (file.exists ()) {//3. If the path points to a file, print the file name if (File.isfile ()) {SYSTEM.O
	            Ut.println (File.getname ()); 4. If the path points to a folder, print all subfolders and files under the folder}else if (File.isdirectory ()) {System.out.println (File.tostrin
	            	g ());
	            	file[] Filearray = File.listfiles ();
	            	    The number of files under the Print folder//system.out.println ("includes" + Filearray.length + "files/folders directly."); Recursively print all files under the folder for (FilE Subfile:filearray) {listfile (subfile.tostring ()); }}else if (!file.isdirectory ()) {//If the path points to a file, print the file name System.out.println (file.t
	        Ostring ());
	        }else{//If the path points to a nonexistent file, print: "(no file)" System.out.println ("no file");
        }}catch (Exception e) {e.printstacktrace ();
       The public static void main (String args[]) {//In the path if the single slash ' \ ', in Java will be considered an escape character, so on the path to all add double slash ' \ '.
       
   ListFile ("c:\\oracle"); }
}

Original address: http://chwshuang.iteye.com/blog/846141

...

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.