zoukankan      html  css  js  c++  java
  • 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对象 涉及到的三个方法:

    
    
  • 相关阅读:
    case class inheritance
    [Akka]发送一条消息的内部流程
    Java 8 新特性
    大数据学习笔记
    磁盘结构
    AtomicReference 和 volatile 的区别
    How to support both ipv4 and ipv6 address for JAVA code.
    使用JProfiler分析定位java内存泄露memory leak
    redhat mount iso as one yum repository
    Linux Shell常用技巧
  • 原文地址:https://www.cnblogs.com/chenhongarticles/p/8995784.html
Copyright © 2011-2022 走看看