forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesAndPaths.java
More file actions
64 lines (60 loc) · 2.22 KB
/
FilesAndPaths.java
File metadata and controls
64 lines (60 loc) · 2.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package JavaDemo.IOTest;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.LinkedList;
import java.util.List;
/**
* @Author MaoTian
* @Classname FilesAndPaths
* @Description Files and Paths
* @Date 上午9:16 2019/8/8
* @Version 1.0
* @Created by mao<[email protected]>
*/
public class FilesAndPaths {
public static void main(String[] args) throws IOException {
Path path=Paths.get("./..///java/src/JavaDemo/paths_files").normalize();
//normalize()
String s1=path.toAbsolutePath().normalize().toString();
System.out.println(s1);
try {
if(!Files.exists(path))
Files.createFile(path);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(Files.getLastModifiedTime(path));
System.out.println(Files.size(path));
System.out.println(Files.isSymbolicLink(path));
System.out.println(Files.isDirectory(path));
System.out.println(Files.readAttributes(path, "*"));
System.out.println("======================================================");
//遍历一个文件夹
Path dir = Paths.get("./..///java/src/JavaDemo/IOTest");
try(DirectoryStream<Path> stream = Files.newDirectoryStream(dir)){
for(Path e : stream){
System.out.println(e.getFileName());
}
}catch(IOException e){
}
//遍历整个文件目录
List<Path> result = new LinkedList<Path>();
Files.walkFileTree(path, new FindJavaVisitor(result));
System.out.println("======================================================");
System.out.println("result.size()=" + result.size());
}
private static class FindJavaVisitor extends SimpleFileVisitor<Path>{
private List<Path> result;
public FindJavaVisitor(List<Path> result){
this.result = result;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs){
if(file.toString().endsWith(".java")){
result.add(file.getFileName());
}
return FileVisitResult.CONTINUE;
}
}
}