Directories in Miranda
Here’s the translation of the Go code to Java, with explanations in Markdown format suitable for Hugo:
Our first program will demonstrate working with directories in the file system. Here’s the full source code:
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 Directories {
private static void check(IOException 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();
if (!created) {
System.out.println("Failed to create directory");
return;
}
// 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");
parentChild.mkdirs();
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
deleteDirectory(new File("subdir"));
}
}
private static void createEmptyFile(String name) throws IOException {
Files.write(Paths.get(name), new byte[0]);
}
private static void deleteDirectory(File directory) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
directory.delete();
}
}To run the program, compile it and use java:
$ javac Directories.java
$ java Directories
Listing subdir/parent
child true
file2 false
file3 false
Listing subdir/parent/child
file4 false
Visiting subdir
subdir true
subdir/file1 false
subdir/parent true
subdir/parent/child true
subdir/parent/child/file4 false
subdir/parent/file2 false
subdir/parent/file3 falseThis Java program demonstrates various operations with directories:
- Creating directories using
mkdir()andmkdirs(). - Creating empty files.
- Listing directory contents.
- Changing the current working directory.
- Recursively walking a directory tree.
Note that Java doesn’t have a direct equivalent to Go’s defer statement for cleanup, so we use a try-finally block to ensure the temporary directory is deleted at the end.
The Files.walkFileTree() method is used to recursively visit all files and directories, which is similar to Go’s filepath.WalkDir().
Remember to handle exceptions appropriately in a real-world application.