Directories in Logo

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

Our first program will work 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 afterwards.
        // 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
            System.out.println("Listing subdir/parent");
            File[] files = new File("subdir/parent").listFiles();
            if (files != null) {
                for (File file : files) {
                    System.out.println(" " + file.getName() + " " + file.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.
            System.out.println("Listing subdir/parent/child");
            files = new File(".").listFiles();
            if (files != null) {
                for (File file : files) {
                    System.out.println(" " + file.getName() + " " + file.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 by deleting the directory and all its contents
            deleteDirectory(new File("subdir"));
        }
    }

    private static void createEmptyFile(String name) {
        try {
            new File(name).createNewFile();
        } catch (IOException e) {
            check(e);
        }
    }

    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();
    }
}

This Java program demonstrates various operations with directories:

  1. It creates a new directory.
  2. It creates a hierarchy of directories.
  3. It creates empty files in these directories.
  4. It lists the contents of directories.
  5. It changes the current working directory.
  6. It walks through a directory tree recursively.
  7. Finally, it cleans up by deleting the created directory and its contents.

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

Note that Java’s file operations are more verbose than Go’s, and error handling is done through exceptions rather than returned error values. Also, Java doesn’t have a direct equivalent to Go’s defer keyword, so we use a try-finally block to ensure cleanup.

The output will be similar to the Go program, showing the created directory structure and the results of directory traversal.