JavaSE练习--IO_File文件模拟杀软

实现效果如下:将目录下的所有内容都展示

请输入要扫描的目录路径:E:\8086
|--8086
    |--DOSBox和Debug安装文件
        |--debug.exe
        |--DOSBox0.74-win32-installer.exe
        |--如何在64位操作系统下使用DEBUG命令.docx
    |--Emu8086v3.07安装文件
        |--emu8086_chs
            |--ReadMe_CHINA.txt
            |--Version-307.txt
            |--_charset.dat
            |--_lang.dat
            |--_msg.dat
            |--中文语言包使用说明.txt
        |--Emu8086_Setup_307c.exe
        |--ReadMe.txt
        |--sn.txt
        |--Version.txt

 1 package com.heqing.main;
 2 
 3 import com.heqing.model.ShowFile;
 4 
 5 import java.io.File;
 6 import java.util.Scanner;
 7 
 8 public class Main {
 9     public static void main(String[] args) {
10         Scanner input = new Scanner(System.in);
11         System.out.print("请输入要扫描的目录路径:");
12         String inputPath = input.nextLine();
13         File file = new File(inputPath);
14         //region 基本信息校验
15         if (!file.exists()){
16             System.out.println("文件夹不存在!");
17             return;
18         }
19         if (!file.isDirectory()){
20             System.out.println("路径指向非文件夹!");
21             return;
22         }
23         //endregion
24         ShowFile.showFile(file,0);
25     }
26 }
 1 package com.heqing.model;
 2 
 3 import java.io.File;
 4 
 5 public class ShowFile {
 6     public static void showFile(File file, int index){
 7         StringBuilder sbCurrent = new StringBuilder();
 8         for (int i = 0; i < index; i++) {
 9             sbCurrent.append('\t');
10         }
11         sbCurrent.append("|--").append(file.getName());
12         System.out.println(sbCurrent);
13         if (file.isDirectory()){
14             File[] files = file.listFiles();
15             for (File file1 : files) {
16                 showFile(file1,(index+1));
17             }
18         }
19     }
20 }
posted @ 2021-08-19 10:28  heqing_echo  阅读(55)  评论(0)    收藏  举报