Walking the File Tree
Walking the File Tree
The FileVisitor Interface
PrintFiles.java
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
public class PrintFiles extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
if (attr.isSymbolicLink()) {
System.out.format("Symbolic link: %s ", file);
} else if (attr.isRegularFile()) {
System.out.format("Regular file: %s ", file);
} else {
System.out.format("other: %s ", file);
}
System.out.println("(" + attr.size() + "bytes)");
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) {
System.out.format("Directory: %s%n", dir);
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException e) {
System.err.println(e);
return CONTINUE;
}
}
kickstarting the Process
KickStart.java
import java.io.*;
import java.nio.file.*;
public class KickStart {
public static void main(String[] args) {
Path startingDir = Paths.get("/tmp");
PrintFiles pf = new PrintFiles();
try {
Files.walkFileTree(startingDir, pf);
} catch (IOException e) {
System.out.println(e);
}
}
}
KickStart2.java
import java.util.*;
import java.io.*;
import java.nio.file.*;
public class KickStart2 {
public static void main(String[] args) {
Path startingDir = Paths.get(".");
PrintFiles pf = new PrintFiles();
try {
// Files.walkFileTree(startingDir, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, pf);
// follow links is show dir in link
Files.walkFileTree(startingDir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, pf);
} catch (IOException e) {
System.out.println(e);
}
}
}
Consideration when Creating a FileVisitor
watch examples
Controlling the Flow
SkipDir.java
import java.nio.file.*;
import java.io.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
public class SkipDir {
public static void main(String[] args) throws IOException {
Path start = Paths.get(".");
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (dir.getFileName().toString().equals("SCCS")) {
return SKIP_SUBTREE;
}
return CONTINUE;
}
@Override
public FileVisitResult visitFile(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("File name: " + dir.getFileName());
return CONTINUE;
}
});
}
}
LocateFile.java
import java.nio.file.*;
import java.io.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
public class LocateFile {
public static void main(String[] args) throws IOException {
Path start = Paths.get(".");
Path lookingFor = Paths.get("Format.java");
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().equals(lookingFor)) {
System.out.println("Located file: " + file);
return TERMINATE;
}
System.out.println("File name: " + file);
return CONTINUE;
}
});
}
}
Examples
相關文章
- CF578E Walking! 題解
- 2024牛客多校第二場 - C. Red Walking on Grid
- tree
- cat > file << EOF 與 cat > file << -
- DSU on Tree
- Rebuild TreeRebuild
- 01 Tree
- Tree Compass
- A - Distance in Tree
- Decision Tree
- 【MySQL(1)| B-tree和B+tree】MySql
- File
- How to Convert Class File to Java File Online?Java
- 多路查詢樹:B-tree/b+tree
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- segment tree beats
- Circular Spanning Tree
- B-tree
- B+tree
- tree-shaking
- Root of AVL Tree
- Tree – Information TheoryORM
- mvn dependency:tree
- Traversals of binary tree
- docker fileDocker
- include "head_file" 和 include <head_file>
- Causal Inference理論學習篇-Tree Based-Causal Tree
- LeetCode C++ 968. Binary Tree Cameras【Tree/DFS】困難LeetCodeC++
- F - Perfect Matching on a Tree
- tmp dbg parse tree
- el-tree-select
- 100. Same Tree
- [leetcode]same-treeLeetCode
- Leetcode Binary Tree PathsLeetCode
- Trie tree實踐
- 100-Same Tree
- 101-Symmetric Tree
- B-tree索引索引