zoukankan      html  css  js  c++  java
  • 定义一个工具类,该类要求用户运行该程序时输入一个路径。该工具类会将该路径下的所有文件列出来

    import java.io.*;
    10 import java.util.Scanner;
    11 
    12 public class filelisttest1{
    13     public static void recv(String path){
    14         File newFile = new File(path);
    15         //当输入exit时退出程序
    16         if(path.equals("exit")){
    17             System.exit(1);
    18         }
    19         //先判断该文件或者目录是否存在
    20         if(newFile.exists()){
    21             //列出File对象的所有子文件和路径,返回File数组
    22             File[] fileList = newFile.listFiles();                  
    23             //判断该文件或者目录是否为空
    24             if(fileList.length == 0){
    25                 System.out.println("该文件为空");
    26             }
    27             for(File file : fileList){
    28                 //判断其是否为文件而不是目录
    29                 if(file.isFile()){
    30                     //返回此File对象所对应的绝对路径名
    31                     System.out.println("文件名"+file.getAbsolutePath   ());
    32                 }
    33                 //如果该File对象是目录
    34                 else{
    35                     System.out.println("文件名"+file.getAbsolutePath   ());
    36                     //使用递归再此将其子目录下的文件的路径打印出来
    37                     recv(file.getAbsolutePath()); 
    38                 }
    39             }
    40         }
    41         else{
    42             System.out.println("该文件不存在");
    43         }
    44     }
    45     public static void main(String[] args){
    46         Scanner sc = new Scanner(System.in);
    47         while(sc.hasNext()){
    48             //获取键盘输入的字符串
    49             recv(sc.next());
    50         }
    51     }
    52 }
    ~                                                             

    运行结果

    笨鸟先飞
  • 相关阅读:
    Java自学
    java自学
    Java自学
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
    每日总结
  • 原文地址:https://www.cnblogs.com/zoutingrong/p/14159774.html
Copyright © 2011-2022 走看看