Reading Files in Fortress

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

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

public class ReadingFiles {
    // Reading and writing files are basic tasks needed for
    // many Java programs. First we'll look at some examples of
    // reading files.

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

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

        // You'll often want more control over how and what
        // parts of a file are read. For these tasks, start
        // by opening a file to obtain a FileInputStream.
        try (FileInputStream f = 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 = f.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.
            f.getChannel().position(6);
            byte[] b2 = new byte[2];
            int n2 = f.read(b2);
            System.out.printf("%d bytes @ %d: ", n2, f.getChannel().position() - n2);
            System.out.printf("%s\n", new String(b2, 0, n2));

            // The seek methods are relative to the current cursor position,
            f.getChannel().position(f.getChannel().position() + 4);

            // and relative to the end of the file.
            f.getChannel().position(f.getChannel().size() - 10);

            // The java.io package provides some classes that may
            // be helpful for file reading. For example, reads
            // like the ones above can be more robustly
            // implemented with BufferedInputStream.
            f.getChannel().position(6);
            BufferedInputStream bis = new BufferedInputStream(f);
            byte[] b3 = new byte[2];
            int n3 = bis.read(b3);
            System.out.printf("%d bytes @ %d: %s\n", n3, f.getChannel().position() - n3, new String(b3));

            // There is no built-in rewind, but
            // seeking to the start accomplishes this.
            f.getChannel().position(0);

            // The BufferedReader class implements a buffered
            // reader that may be useful both for its efficiency
            // with many small reads and because of the additional
            // reading methods it provides.
            BufferedReader r4 = new BufferedReader(new InputStreamReader(f));
            char[] b4 = new char[5];
            r4.mark(5);
            r4.read(b4, 0, 5);
            System.out.printf("5 bytes: %s\n", new String(b4));
            r4.reset();

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

To run this program:

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

This Java code demonstrates various methods of reading files, including reading entire files, seeking to specific positions, and using buffered readers. The structure and explanations are similar to the original, but adapted for Java’s file I/O APIs.

Next, we’ll look at writing files.