Directories in Squirrel

Here’s the translation of the Go code to Java, with explanations in Markdown format suitable for Hugo:

Java provides several useful classes 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 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.
        try {
            Files.createDirectory(Paths.get("subdir"));
        } catch (IOException e) {
            check(e);
        }

        // When creating temporary directories, it's good practice to delete them when done.
        // We'll use a try-with-resources to ensure cleanup.
        try {
            Path tempDir = Files.createTempDirectory("temp");
            try {
                // Helper method to create a new empty file.
                createEmptyFile(tempDir.resolve("file1").toString());

                // We can create a hierarchy of directories, including parents with createDirectories.
                // This is similar to the command-line `mkdir -p`.
                Files.createDirectories(tempDir.resolve("parent/child"));

                createEmptyFile(tempDir.resolve("parent/file2").toString());
                createEmptyFile(tempDir.resolve("parent/file3").toString());
                createEmptyFile(tempDir.resolve("parent/child/file4").toString());

                // list lists directory contents.
                System.out.println("Listing " + tempDir.resolve("parent"));
                try (var stream = Files.list(tempDir.resolve("parent"))) {
                    stream.forEach(path -> System.out.println(" " + path.getFileName() + " " + Files.isDirectory(path)));
                }

                // Changing the current working directory is not recommended in Java,
                // but we can use absolute paths instead.
                Path childDir = tempDir.resolve("parent/child");

                System.out.println("Listing " + childDir);
                try (var stream = Files.list(childDir)) {
                    stream.forEach(path -> System.out.println(" " + path.getFileName() + " " + Files.isDirectory(path)));
                }

                // We can also visit a directory recursively, including all its sub-directories.
                // Files.walkFileTree accepts a FileVisitor to handle every file or directory visited.
                System.out.println("Visiting " + tempDir);
                Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        System.out.println(" " + file + " " + attrs.isDirectory());
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                        System.out.println(" " + dir + " " + attrs.isDirectory());
                        return FileVisitResult.CONTINUE;
                    }
                });

            } finally {
                // Clean up the temporary directory
                deleteDirectory(tempDir.toFile());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void createEmptyFile(String name) throws IOException {
        Files.write(Paths.get(name), new byte[0]);
    }

    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 with directories:

  1. It creates a new directory using Files.createDirectory().
  2. It creates a temporary directory using Files.createTempDirectory().
  3. It creates empty files using a helper method.
  4. It creates a hierarchy of directories using Files.createDirectories().
  5. It lists directory contents using Files.list().
  6. It recursively visits a directory structure using Files.walkFileTree().

Note that Java doesn’t have a direct equivalent to Go’s os.Chdir(). Changing the current working directory is generally discouraged in Java. Instead, we use absolute paths.

Also, Java doesn’t have a built-in method to recursively delete a directory, so we’ve implemented a custom deleteDirectory method.

To run this program, save it as Directories.java, compile it with javac Directories.java, and run it with java Directories.