File Paths in Minitab

Here’s the translation of the Go code to Java, with explanations in Markdown format 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. It handles differences like 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) {
        // The Paths.get() method 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 get the extension out of such names with getExtension().
        String ext = getExtension(filename);
        System.out.println(ext);

        // To find the file's name with the extension removed,
        // use removeExtension().
        System.out.println(removeExtension(filename));

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

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

    private static String getExtension(String filename) {
        int dotIndex = filename.lastIndexOf('.');
        return (dotIndex == -1) ? "" : filename.substring(dotIndex);
    }

    private static String removeExtension(String filename) {
        int dotIndex = filename.lastIndexOf('.');
        return (dotIndex == -1) ? filename : filename.substring(0, dotIndex);
    }
}

To run the 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.

This Java code demonstrates the usage of the java.nio.file.Paths class for handling file paths in a cross-platform manner. It covers path construction, normalization, path component extraction, absolute path checking, and relative path calculation. The getExtension() and removeExtension() methods are custom implementations to handle file extensions, as Java doesn’t provide built-in methods for these operations.