-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFileDemo7.java
More file actions
45 lines (39 loc) · 1.22 KB
/
FileDemo7.java
File metadata and controls
45 lines (39 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package code_00_disk;
import java.io.File;
/**
* Created by 18351 on 2019/1/4.
*/
public class FileDemo7 {
public static void main(String[] args) {
// 指定一个目录
File file = new File("src\\code_00_disk");
// public String[] list():获取指定目录下的所有文件或者文件夹的名称数组
String[] strArray = file.list();
for (String s : strArray) {
System.out.println(s);
}
System.out.println("------------");
// public File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
File[] fileArray = file.listFiles();
if(fileArray!=null){
for (File f : fileArray) {
System.out.println(f.getName());
}
}
System.out.println("--------------");
listAllFiles(file);
}
//递归地列出一个目录下所有文件
public static void listAllFiles(File dir) {
if (dir == null || !dir.exists()) {
return;
}
if (dir.isFile()) {
System.out.println(dir.getName());
return;
}
for (File file : dir.listFiles()) {
listAllFiles(file);
}
}
}