首页 > 编程语言 > 详细

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

时间:2018-05-05 20:26:38      阅读:234      评论:0      收藏:0      [点我收藏+]
 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对象 涉及到的三个方法:

技术分享图片

技术分享图片

技术分享图片

 

 

 

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

原文:https://www.cnblogs.com/chenhongarticles/p/8995784.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!