Title here
Summary here
Here’s the translation of the Go code for working with directories to Java, along with explanations in Markdown format suitable for Hugo:
Our Java program demonstrates various operations for working with directories in the file system.
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DirectoryOperations {
private static void check(Exception e) {
if (e != null) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
// Create a new sub-directory in the current working directory.
File subdir = new File("subdir");
boolean created = subdir.mkdir();
check(created ? null : new IOException("Failed to create directory"));
// When creating temporary directories, it's good practice to delete them when done.
// We'll use a try-with-resources to ensure cleanup.
try {
// Helper method to create a new empty file.
createEmptyFile("subdir/file1");
// We can create a hierarchy of directories, including parents.
// This is similar to the command-line `mkdir -p`.
File parentChild = new File("subdir/parent/child");
created = parentChild.mkdirs();
check(created ? null : new IOException("Failed to create directories"));
createEmptyFile("subdir/parent/file2");
createEmptyFile("subdir/parent/file3");
createEmptyFile("subdir/parent/child/file4");
// list directory contents
File parentDir = new File("subdir/parent");
System.out.println("Listing subdir/parent");
for (File entry : parentDir.listFiles()) {
System.out.println(" " + entry.getName() + " " + entry.isDirectory());
}
// Change the current working directory
System.setProperty("user.dir", "subdir/parent/child");
// Now we'll see the contents of subdir/parent/child
// when listing the current directory.
File currentDir = new File(".");
System.out.println("Listing subdir/parent/child");
for (File entry : currentDir.listFiles()) {
System.out.println(" " + entry.getName() + " " + entry.isDirectory());
}
// Change back to where we started
System.setProperty("user.dir", "../../..");
// We can also visit a directory recursively,
// including all its sub-directories.
System.out.println("Visiting subdir");
Files.walkFileTree(Paths.get("subdir"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(" " + file + " " + Files.isDirectory(file));
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println(" " + dir + " " + Files.isDirectory(dir));
return FileVisitResult.CONTINUE;
}
});
} finally {
// Clean up: delete the temporary directory and all its contents
deleteDirectory(new File("subdir"));
}
}
private static void createEmptyFile(String name) throws IOException {
File file = new File(name);
file.getParentFile().mkdirs();
file.createNewFile();
}
private static void deleteDirectory(File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
dir.delete();
}
}
This Java program demonstrates various operations for working with directories:
File.mkdir()
.File.mkdirs()
.File.listFiles()
.System.setProperty("user.dir", ...)
.Files.walkFileTree()
.To run this program, save it as DirectoryOperations.java
and use the following commands:
$ javac DirectoryOperations.java
$ java DirectoryOperations
The output will show the directory listings and the recursive visit of the directory structure.
Remember that working with files and directories can throw IOExceptions, so in a production environment, you should handle these exceptions appropriately.