.Enter a subdirectory of the file system, and the program will output the names of all the contents in it

Enter a subdirectory of the file system, and the program will output the names of all the contents in it to the console, requiring:

a. Use a generic dynamic array as a tool;
b. Use the File object and its methods on page 193 of the textbook;
c. Output the file names of all subdirectories (may have many layers) and all files in this subdirectory ;
d. When outputting directory names and file names, use prefix spaces to display hierarchical relationships;

import java.util.*;
import java.io.*;
import java.io.*;

public class OutputFiles {
    
    
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		ArrayList<File> files=new ArrayList<File>();
		File myFile=new File("D:\\Documents\\FileRecv\\学习");
		files.add(myFile);
		for(int i=0;i<files.size();i++)
		{
    
    
		 if(files.get(i).isDirectory())
		 {
    
    File[] ListFiles=files.get(i).listFiles();
		  for(int j=0;j<ListFiles.length;j++)
		  {
    
    files.add(ListFiles[j]);
		  }
		 }
		}
		for(int i=0;i<files.size();i++)
		{
    
    
			System.out.println(files.get(i).getName());
		}
}
}

insert image description here

Guess you like

Origin blog.csdn.net/qq_45808700/article/details/118156638