File Paths in Karel

Here’s the translation of the Go code example to Java, formatted in Markdown suitable for Hugo:

The java.nio.file.Paths class provides methods to work with file paths in a way that is portable between operating systems. For example, dir/file on Linux vs. dir\file on Windows.

import java.nio.file.Path;
import java.nio.file.Paths;

public class FilePaths {
    public static void main(String[] args) {
        // Paths.get() should be used to construct paths in a portable way.
        // It takes any number of arguments and constructs a hierarchical path from them.
        Path p = Paths.get("dir1", "dir2", "filename");
        System.out.println("p: " + p);

        // You should always use Paths.get() instead of concatenating
        // "/" or "\" manually. In addition to providing portability,
        // Paths.get() will also normalize paths by removing superfluous
        // separators and directory changes.
        System.out.println(Paths.get("dir1//", "filename"));
        System.out.println(Paths.get("dir1/../dir1", "filename"));

        // getParent() and getFileName() can be used to split a path to the
        // directory and the file.
        System.out.println("p.getParent(): " + p.getParent());
        System.out.println("p.getFileName(): " + p.getFileName());

        // We can check whether a path is absolute.
        System.out.println(Paths.get("dir/file").isAbsolute());
        System.out.println(Paths.get("/dir/file").isAbsolute());

        String filename = "config.json";

        // Some file names have extensions following a dot. We can
        // split the extension out of such names.
        int dotIndex = filename.lastIndexOf('.');
        String ext = (dotIndex == -1) ? "" : filename.substring(dotIndex);
        System.out.println(ext);

        // To find the file's name with the extension removed,
        // use String's substring method.
        System.out.println(filename.substring(0, filename.length() - ext.length()));

        // relativize() finds a relative path between a base and a target.
        // It returns the path to get to the target from the base.
        Path base = Paths.get("a/b");
        Path target = Paths.get("a/b/t/file");
        System.out.println(base.relativize(target));

        base = Paths.get("a/b");
        target = Paths.get("a/c/t/file");
        System.out.println(base.relativize(target));
    }
}

To run this program:

$ javac FilePaths.java
$ java FilePaths
p: dir1\dir2\filename
dir1\filename
dir1\filename
p.getParent(): dir1\dir2
p.getFileName(): filename
false
true
.json
config
t\file
..\c\t\file

Note: The output may vary slightly depending on the operating system due to different path separators.

Java’s java.nio.file.Paths class provides similar functionality to Go’s filepath package. The Paths.get() method is used to construct paths, which is analogous to filepath.Join() in Go. Methods like getParent() and getFileName() are used to split paths, similar to filepath.Dir() and filepath.Base() in Go.

The isAbsolute() method checks if a path is absolute, just like filepath.IsAbs() in Go. For getting file extensions and trimming them, Java uses string manipulation methods as it doesn’t have built-in functions for these operations.

The relativize() method in Java is similar to filepath.Rel() in Go, finding the relative path between two paths.

Remember that Java’s path handling might behave differently on different operating systems, especially regarding the path separator (\ on Windows, / on Unix-like systems).