• Java读取指定目录下的所有文件(子目录里的文件也可递归得到)


     1 import java.io.File;
     2 
     3 public class ReadFile {
     4 
     5     public static void main(String[] args) {
     6 
     7         //     path是指定目录的绝对路径
     8         String path = "/Users/tonychan/Workspaces/MyEclipse 2017 CI/Zhangjiajie/WebRoot/pics";
     9         getFile(path);    
    10 
    11     }
    12 
    13     // 给定目录的绝对路径,获取该目录下的所有文件(子目录的文件也可递归得到)
    14     public static void getFile(String path) {
    15         // File对象 可以是文件或者目录
    16         File file = new File(path);
    17         File[] array = file.listFiles();
    18 
    19         for (int i = 0; i < array.length; i++) {
    20             if (array[i].isFile()) {
    21                 // only take file name
    22                 System.out.println("^^^^^" + array[i].getName());
    23                 // take file path and name
    24                 System.out.println("#####" + array[i]);
    25                 // take file path and name
    26                 System.out.println("*****" + array[i].getPath());
    27             } else if (array[i].isDirectory()) {
    28                  getFile(array[i].getPath());
    29             }
    30         }
    31     }
    32 
    33 }

     1. Mac下的路径里的空格,在Java中不用转义

    String path = "/Users/tonychan/Workspaces/MyEclipse 2017 CI/Zhangjiajie/WebRoot/pics";

     2. File对象 可以是文件或者目录

     3. File对象 涉及到的三个方法:

    
    
  • 相关阅读:
    C语言运算符优先级和口诀
    跨域问题的解决方案 php
    浅谈跨域攻击及预防
    浅析Websocket--PHP
    linux下的删除目录和文件的方法
    python魔法方法
    双指针
    python常用模块
    python三大器
    对闭包的误区
  • 原文地址:https://www.cnblogs.com/chenhongarticles/p/8995784.html
Copyright © 2020-2023  润新知