Display folders in a tree structure

Display folders in a tree structure


Subject requirements: Write a program to display a specific folder and its subfolders in a tree structure on the command line. If the subfile is a file, you need to display the file size, and finally count the size of the entire directory
public class Test {
    
     
	static long fileNum=0; 
	static long fileLen=0; 
	public static void main(String[] args) {
    
     
		String ss="C:\\windows"; 
		File ff=new File(ss); 
			if(ff != null && ff.exists()){
    
     		
				System.out.println(ff.getParent().getAbsolutePath()); 
				showFile(ff,0); 
			}
			System.out.println(ff.getAbsolutePath()+"的文件数目为:"+fileNum+",总大小 为:"+fileLen); 
	}
	public static void showFile(File ff,int level) {
    
     
		if(ff != null) {
    
     
			for(int i = 0;i<level;i++) 
				System.out.print(" "); 
			System.out.print("|-"); 
			if(ff.isFile()) {
    
     
				fileNum++; 
				fileLen+=ff.length(); 
				System.out.println(ff.getName()+":"+ff.length()); 
			}else if(ff.isDirectory()) {
    
     
				System.out.println(ff.getName());
				File[] fs=ff.listFiles(); 
				for(File temp:fs)
					showFile(temp, level+1); 
			} 
		} 
	} 
}

Guess you like

Origin blog.csdn.net/qq_43480434/article/details/113356968