I had a chance to implement the process of recursively deleting the directory, that is, the process equivalent to rm -rf
in Java, so I will leave a note (´ ・ ω ・ `) Not limited to this process Files.walkTree
is useful if you want to process directories recursively. Files.walkTree
easily implements recursive processing by giving the path of the directory to be processed as the first argument and FileVisitor
which defines the behavior when a file or directory is detected as the second argument. can.
Now, FileVisitor
is an interface that requires the implementation of four methods, but if you inherit the SimpleFileVisitor
class, you can easily achieve the purpose by simply overriding the required methods. For example, you can create a FileVisitor
that recursively traverses a directory and deletes all files and directories as follows:
RemoveRecurseFileVisitor.java
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class RemoveRecurseFileVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
return delete(file);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
return delete(dir);
}
private FileVisitResult delete(Path path) {
try {
Files.deleteIfExists(path);
System.out.printf("removed '%s'%n", path);
return FileVisitResult.CONTINUE;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Now let's actually move the created code. First, execute the following series of commands in order.
mkdir -p dir01/dir02/dir03
touch dir01/file1
touch dir01/dir02/file2
touch dir01/dir02/file3
touch dir01/dir02/dir03/file4
touch dir01/dir02/dir03/file5
touch dir01/dir02/dir03/file6
This should create a directory file similar to the following:
$ find dir1/ -exec stat -c '%n (%F)' {} \;
dir1/ (directory)
dir1/dir2 (directory)
dir1/dir2/dir3 (directory)
dir1/dir2/dir3/txt4 (regular empty file)
dir1/dir2/dir3/txt5 (regular empty file)
dir1/dir2/dir3/txt6 (regular empty file)
dir1/dir2/txt2 (regular empty file)
dir1/dir2/txt3 (regular empty file)
dir1/txt1 (regular empty file)
Now use RemoveRecurseFileVisitor
to create Main.java
to remove dir1
and the files and directories under it.
Main.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws IOException {
var target = Paths.get("dir1");
Files.walkFileTree(target, new RemoveRecurseFileVisitor());
}
}
After that, if you compile and execute this, you should be able to confirm that the deletion log is output to the standard output first (´ ・ ω ・ `)
removed 'dir1\dir2\dir3\txt4'
removed 'dir1\dir2\dir3\txt5'
removed 'dir1\dir2\dir3\txt6'
removed 'dir1\dir2\dir3'
removed 'dir1\dir2\txt2'
removed 'dir1\dir2\txt3'
removed 'dir1\dir2'
removed 'dir1\txt1'
removed 'dir1'
You can confirm that the directory dir1
and everything under it have been deleted with a command like the following.
$ ls -l dir1
ls: cannot access 'dir1': No such file or directory
Reference: https://docs.oracle.com/javase/tutorial/essential/io/walk.html
Recommended Posts