Java : FileVisitor con ejemplos
FileVisitor (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos FileVisitor<T>.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//└─target-dir
// ├─dir1
// │ aaa.txt
// │ bbb.txt
// │
// └─dir2
// ccc.txt
// ddd.txt
// An example to delete a target directory with contents.
final FileVisitor<Path> visitor = new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("visitFile : " + file);
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("postVisitDirectory : " + dir);
if (exc != null) {
throw exc;
}
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
};
final var targetDir = Path.of("R:", "java-work", "target-dir");
System.out.println("target dir : " + targetDir);
System.out.println("exists : " + Files.exists(targetDir));
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(targetDir, visitor);
System.out.println("-- walkFileTree : end --");
System.out.println("exists : " + Files.exists(targetDir));
// Result
// ↓
//target dir : R:\java-work\target-dir
//exists : true
//-- walkFileTree : start --
//visitFile : R:\java-work\target-dir\dir1\aaa.txt
//visitFile : R:\java-work\target-dir\dir1\bbb.txt
//postVisitDirectory : R:\java-work\target-dir\dir1
//visitFile : R:\java-work\target-dir\dir2\ccc.txt
//visitFile : R:\java-work\target-dir\dir2\ddd.txt
//postVisitDirectory : R:\java-work\target-dir\dir2
//postVisitDirectory : R:\java-work\target-dir
//-- walkFileTree : end --
//exists : false
Methods
FileVisitResult postVisitDirectory (T dir, IOException exc)
// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│ aaa.txt
//│ bbb.txt
//│
//├─dir1
//│ │ ccc.txt
//│ │ ddd.txt
//│ │
//│ └─dir1-2
//│ eee.txt
//│ fff.txt
//│
//└─dir2
// ggg.txt
// hhh.txt
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);
final var visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println("preVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("visitFile : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("visitFileFailed : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("postVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
};
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);
System.out.println("-- walkFileTree : end --");
// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory : R:\java-work
//visitFile : R:\java-work\aaa.txt
//visitFile : R:\java-work\bbb.txt
//preVisitDirectory : R:\java-work\dir1
//visitFile : R:\java-work\dir1\ccc.txt
//visitFile : R:\java-work\dir1\ddd.txt
//preVisitDirectory : R:\java-work\dir1\dir1-2
//visitFile : R:\java-work\dir1\dir1-2\eee.txt
//visitFile : R:\java-work\dir1\dir1-2\fff.txt
//postVisitDirectory : R:\java-work\dir1\dir1-2
//postVisitDirectory : R:\java-work\dir1
//preVisitDirectory : R:\java-work\dir2
//visitFile : R:\java-work\dir2\ggg.txt
//visitFile : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);
final var visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println("preVisitDirectory : " + dir);
if (Path.of("dir1").equals(dir.getFileName())) {
System.out.println(" dir1 SKIP!");
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("visitFile : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("visitFileFailed : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("postVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
};
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);
System.out.println("-- walkFileTree : end --");
// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory : R:\java-work
//visitFile : R:\java-work\aaa.txt
//visitFile : R:\java-work\bbb.txt
//preVisitDirectory : R:\java-work\dir1
// dir1 SKIP!
//preVisitDirectory : R:\java-work\dir2
//visitFile : R:\java-work\dir2\ggg.txt
//visitFile : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
FileVisitResult preVisitDirectory (T dir, BasicFileAttributes attrs)
// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│ aaa.txt
//│ bbb.txt
//│
//├─dir1
//│ │ ccc.txt
//│ │ ddd.txt
//│ │
//│ └─dir1-2
//│ eee.txt
//│ fff.txt
//│
//└─dir2
// ggg.txt
// hhh.txt
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);
final var visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println("preVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("visitFile : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("visitFileFailed : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("postVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
};
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);
System.out.println("-- walkFileTree : end --");
// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory : R:\java-work
//visitFile : R:\java-work\aaa.txt
//visitFile : R:\java-work\bbb.txt
//preVisitDirectory : R:\java-work\dir1
//visitFile : R:\java-work\dir1\ccc.txt
//visitFile : R:\java-work\dir1\ddd.txt
//preVisitDirectory : R:\java-work\dir1\dir1-2
//visitFile : R:\java-work\dir1\dir1-2\eee.txt
//visitFile : R:\java-work\dir1\dir1-2\fff.txt
//postVisitDirectory : R:\java-work\dir1\dir1-2
//postVisitDirectory : R:\java-work\dir1
//preVisitDirectory : R:\java-work\dir2
//visitFile : R:\java-work\dir2\ggg.txt
//visitFile : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);
final var visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println("preVisitDirectory : " + dir);
if (Path.of("dir1").equals(dir.getFileName())) {
System.out.println(" dir1 SKIP!");
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("visitFile : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("visitFileFailed : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("postVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
};
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);
System.out.println("-- walkFileTree : end --");
// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory : R:\java-work
//visitFile : R:\java-work\aaa.txt
//visitFile : R:\java-work\bbb.txt
//preVisitDirectory : R:\java-work\dir1
// dir1 SKIP!
//preVisitDirectory : R:\java-work\dir2
//visitFile : R:\java-work\dir2\ggg.txt
//visitFile : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
FileVisitResult visitFile (T file, BasicFileAttributes attrs)
// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//│ aaa.txt
//│ bbb.txt
//│
//├─dir1
//│ │ ccc.txt
//│ │ ddd.txt
//│ │
//│ └─dir1-2
//│ eee.txt
//│ fff.txt
//│
//└─dir2
// ggg.txt
// hhh.txt
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);
final var visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println("preVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("visitFile : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("visitFileFailed : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("postVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
};
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);
System.out.println("-- walkFileTree : end --");
// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory : R:\java-work
//visitFile : R:\java-work\aaa.txt
//visitFile : R:\java-work\bbb.txt
//preVisitDirectory : R:\java-work\dir1
//visitFile : R:\java-work\dir1\ccc.txt
//visitFile : R:\java-work\dir1\ddd.txt
//preVisitDirectory : R:\java-work\dir1\dir1-2
//visitFile : R:\java-work\dir1\dir1-2\eee.txt
//visitFile : R:\java-work\dir1\dir1-2\fff.txt
//postVisitDirectory : R:\java-work\dir1\dir1-2
//postVisitDirectory : R:\java-work\dir1
//preVisitDirectory : R:\java-work\dir2
//visitFile : R:\java-work\dir2\ggg.txt
//visitFile : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);
final var visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println("preVisitDirectory : " + dir);
if (Path.of("dir1").equals(dir.getFileName())) {
System.out.println(" dir1 SKIP!");
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("visitFile : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("visitFileFailed : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("postVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
};
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);
System.out.println("-- walkFileTree : end --");
// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory : R:\java-work
//visitFile : R:\java-work\aaa.txt
//visitFile : R:\java-work\bbb.txt
//preVisitDirectory : R:\java-work\dir1
// dir1 SKIP!
//preVisitDirectory : R:\java-work\dir2
//visitFile : R:\java-work\dir2\ggg.txt
//visitFile : R:\java-work\dir2\hhh.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --
FileVisitResult visitFileFailed (T file, IOException exc)
// --- PowerShell ---
//PS R:\java-work> tree /F
//...
//R:.
//├─dir1
//└─dir2
// aaa.txt
//
//PS R:\java-work> ls | Get-Acl
//
// Directory: R:\java-work
//
//Path Owner Access
//---- ----- ------
//dir1 MY-PC\test-user BUILTIN\Administrators Deny ReadData…
//dir2 MY-PC\test-user BUILTIN\Administrators Allow FullControl…
// Intentionally deny read permission to the dir1 directory.
final var start = Path.of("R:", "java-work");
System.out.println("start dir : " + start);
final var visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println("preVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println("visitFile : " + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println("visitFileFailed : " + file);
System.out.println("IOException : " + exc);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("postVisitDirectory : " + dir);
return FileVisitResult.CONTINUE;
}
};
System.out.println("-- walkFileTree : start --");
Files.walkFileTree(start, visitor);
System.out.println("-- walkFileTree : end --");
// Result
// ↓
//start dir : R:\java-work
//-- walkFileTree : start --
//preVisitDirectory : R:\java-work
//visitFileFailed : R:\java-work\dir1
//IOException : java.nio.file.AccessDeniedException: R:\java-work\dir1
//preVisitDirectory : R:\java-work\dir2
//visitFile : R:\java-work\dir2\aaa.txt
//postVisitDirectory : R:\java-work\dir2
//postVisitDirectory : R:\java-work
//-- walkFileTree : end --