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
...