zoukankan      html  css  js  c++  java
  • 遍历目录下的所有文件,同时获取文件的类型

    遍历指定目录下的所有文件,同时获取文件的类型,即后缀名。

        public static void main(String[] args) {
            String pathName = "D:\testPath";
            showFileName(pathName);
        }
    
         public static void showFileName(String path) {
            File file = new File(path);
            if (file.exists()) {
                File[] files = file.listFiles();
                if (null != files) {
                    for (File innerFile : files) {
                        if (innerFile.isDirectory()) {
                            System.out.println("文件夹:" + innerFile.getAbsolutePath());
                            showFileName(innerFile.getAbsolutePath());
                        } else {
                            System.out.println("文件:" + innerFile.getAbsolutePath());
                            String fileName = innerFile.getName();
                            //获取文件的后缀名
                            String suffix = fileName.substring(fileName.lastIndexOf("."));
                            System.out.println(fileName + ", suffix = " + suffix);
                        }
                    }
                }
            } else {
                System.out.println("文件不存在!");
            }
        }
    

      读后有收获,小礼物走一走,请作者喝咖啡。

    赞赏支持

  • 相关阅读:
    Jquery入门
    微服务
    数组
    流程控制
    GO的整型
    Go的巧记
    变量和常量
    Golang
    股票入市指南
    linux 命令行操作
  • 原文地址:https://www.cnblogs.com/east7/p/15023563.html
Copyright © 2011-2022 走看看