public class Printoutfile {public static void main (string[] args) {Printfile (newFile("D:\\test"), 1); public static void Printfile (file File,int sub) {if (file.Isfile()) {System.out.println ("You are given a file");//Determine if a given directory is a valid directory, if not, output prompt} else {file[] FileList s = file.Listfiles(); If it is a directory, get the content collection for this directory for (int i = 0; i < filelists.length; i++) {//Loop through this collection content for (int j = 0; J < Sub; J + +) {//output indent, in order to separate folders and files System.out.print ("|---") in the printed result central; } System.out.println (Filelists[i].GetName()); The output element name if (Filelists[i].isdirectory()) {//The judging element is not a directory printfile (filelists[i],sub+1); If it is a directory, continue calling this method to output its subdirectories, because it is its subdirectory, so the number of indents + 1}}}
Java prints out all subfolders and files in a folder under a specified path and differentiates