Reading Files in Minitab

Here’s the translation of the Go code for reading files into Java, formatted in Markdown suitable for Hugo:

Our first example will demonstrate various methods of reading files in Java. Reading files is a common task in many Java programs.

import java.io.*;
import java.nio.file.*;

public class ReadingFiles {
    // This helper method will streamline our error checks
    private static void check(Exception e) {
        if (e != null) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        try {
            // Perhaps the most basic file reading task is
            // slurping a file's entire contents into memory.
            String content = new String(Files.readAllBytes(Paths.get("/tmp/dat")));
            System.out.print(content);

            // You'll often want more control over how and what
            // parts of a file are read. For these tasks, start
            // by creating a FileInputStream.
            FileInputStream fis = new FileInputStream("/tmp/dat");

            // Read some bytes from the beginning of the file.
            // Allow up to 5 to be read but also note how many
            // actually were read.
            byte[] b1 = new byte[5];
            int n1 = fis.read(b1);
            System.out.printf("%d bytes: %s\n", n1, new String(b1, 0, n1));

            // You can also seek to a known location in the file
            // and read from there.
            fis.getChannel().position(6);
            byte[] b2 = new byte[2];
            int n2 = fis.read(b2);
            System.out.printf("%d bytes @ %d: ", n2, 6);
            System.out.printf("%s\n", new String(b2, 0, n2));

            // The seek method is relative to the current position
            fis.getChannel().position(fis.getChannel().position() + 4);

            // And you can seek relative to the end of the file.
            fis.getChannel().position(fis.getChannel().size() - 10);

            // The BufferedReader class provides more efficient reading
            // for many small reads, and additional reading methods.
            fis.getChannel().position(0);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            char[] b4 = new char[5];
            reader.mark(5);
            int n4 = reader.read(b4, 0, 5);
            reader.reset();
            System.out.printf("5 bytes: %s\n", new String(b4, 0, n4));

            // Close the file when you're done (usually this would
            // be scheduled immediately after opening with try-with-resources).
            fis.close();

        } catch (IOException e) {
            check(e);
        }
    }
}

To run this program, you would first create a file /tmp/dat with some content:

$ echo "hello" > /tmp/dat
$ echo "java" >> /tmp/dat
$ javac ReadingFiles.java
$ java ReadingFiles
hello
java
5 bytes: hello
2 bytes @ 6: ja
5 bytes: hello

This example demonstrates various methods of reading files in Java, including reading entire files, reading specific portions, seeking to different positions, and using buffered readers for efficiency.

Java provides robust file I/O capabilities through classes in the java.io and java.nio.file packages. The Files class offers convenient methods for common operations, while FileInputStream and BufferedReader allow for more fine-grained control over file reading.

Remember to always close your file resources when you’re done with them. In modern Java, it’s recommended to use try-with-resources statements to automatically close resources.

Next, we’ll look at writing files.