Reading Files in Logo

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

Our first example will demonstrate various ways to read 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) {
        // 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 creating a FileInputStream.
        try (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 skip to a known location in the file
            // and read from there.
            long o2 = fis.skip(1);
            byte[] b2 = new byte[2];
            int n2 = fis.read(b2);
            System.out.printf("%d bytes @ %d: ", n2, o2);
            System.out.printf("%s\n", new String(b2, 0, n2));

            // The IOException that may be thrown contains methods
            // for inspecting errors in more detail.
            try {
                fis.skip(4);
            } catch (IOException e) {
                System.out.println(e);
            }

            // There is no built-in rewind, but you can
            // use seek to accomplish this.
            ((FileInputStream) fis).getChannel().position(0);

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

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

To run this program, first create a file with some content:

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

This example demonstrates various methods of reading files in Java:

  1. We start by reading the entire file content using Files.readAllBytes().
  2. Then we use FileInputStream for more controlled reading, including reading specific numbers of bytes and skipping to certain positions.
  3. We show how to use BufferedReader for more efficient reading of small chunks and demonstrate its mark() and reset() capabilities.
  4. Error handling is done through a check() method that throws a RuntimeException if an error occurs.

Note that Java uses exceptions for error handling, unlike Go’s error return values. Also, Java’s try-with-resources statement (used with FileInputStream) automatically closes the resource when we’re done, similar to Go’s defer.

In the next example, we’ll look at writing files.