zoukankan      html  css  js  c++  java
  • 递归查看文件目录下所有文件

     1 import java.io.File;
     2 
     3 public class 文件 {
     4 
     5     /**
     6      * 作用 主方法,程序的入口
     7      * 
     8      * @param args
     9      */
    10     public static void main(String[] args) {
    11         // TODO Auto-generated method stub
    12         getFileAll("E:/",0);
    13     }
    14 
    15     /**
    16      * 作用: 获取文件或目录名称
    17      * 
    18      * @param fileName
    19      *            文件路径
    20      * @return 文件名
    21      */
    22     public static String getFileName(String fileName) {
    23         File file = new File(fileName); // 创建文件对象
    24         return file.getName(); // 文件名
    25     }
    26 
    27     /**
    28      * 作用:获取文件权限
    29      * 
    30      * @param fileName
    31      *            文件路径
    32      * @return 文件权限
    33      */
    34     private static String getFileCan(String fileName) {
    35         File file = new File(fileName); // 创建文件对象
    36         return (file.canRead() ? "r" : "-") // 文件可读权限判断
    37                 + (file.canWrite() ? "w" : "-") // 文件可写权限判断
    38                 + (file.canExecute() ? "x" : "-"); // 文件可执行权限判断
    39     }
    40 
    41     /**
    42      * 作用: 获取文件大小
    43      * 
    44      * @param fileName
    45      *            文件路径
    46      * @return 返回文件大小
    47      */
    48     private static float getFileSzie(String fileName) {
    49         File file = new File(fileName); // 创建文件对象
    50         return file.length() / 1024F;// 返回文件大小
    51     }
    52 
    53     /**
    54      * @param fileName  文件路径
    55      * @param num 文件深度
    56      */
    57     private static void getFileAll(String fileName,int num) {
    58         File file = new File(fileName); // 创建文件对象
    59         
    60         //验证路径是否存在
    61         if (file.exists()) {//存在
    62             for (File File : file.listFiles()) {// 循环获取目录下文件
    63                 int newNum=num;//使同级文件深度相同
    64                 for (int i = 0; i <=newNum; i++) {
    65                     System.out.print("  ");//打印深度空格
    66                 }
    67                 // 判断是否为目录
    68                 String filePath=File.getAbsolutePath();
    69                                 
    70                 if (File.isDirectory()) {
    71                     System.out.println("目录名:"+File.getName()+"   权限:"+getFileCan(filePath));
    72                     newNum++;//递归次数+1
    73                     getFileAll(filePath,newNum);//getAbsolutePath() 获取文件路径
    74                     
    75                 }else {                
    76                     System.out.println("文件名:"+getFileName(filePath)+"   权限:"+getFileCan(filePath)+"  大小:"+getFileSzie(filePath)+"kb");                    
    77                 }
    78             }
    79         } else {
    80             System.out.println("找不到此目录!");
    81         }
    82     
    83     }
    84 }
  • 相关阅读:
    Scrum 冲刺博客第五篇
    Scrum 冲刺博客第四篇
    Scrum 冲刺博客第三篇
    ajax send()
    form action中get post传递参数的问题
    struts2 iterator中if标签的使用
    表格内容自动换行
    从js向Action传中文参数出现乱码问题的解决方法
    java开发环境搭建
    Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/
  • 原文地址:https://www.cnblogs.com/sylwh/p/7220681.html
Copyright © 2011-2022 走看看