7. Read a file safely

Keep Counting.kalm, Statistics.kalm, and Summary.kalm. Add src/main/kalm/stats/Input.kalm:

Input.kalm
space stats

use java.nio.file.Files
use java.nio.file.Path

pub fun readLines(path: Path) -> List<Text>:
    fix lines: List<Text> = []
    try:
        with reader = Files.newBufferedReader(path):
            while true:
                fix line: Text? = reader.readLine()
                if line != null:
                    lines.add(line)
                else:
                    stop
    handle (error: java.io.IOException):
        sayLine("Could not read file")
        exit(1)

    return lines
Input.kalm
space stats

use java.nio.file.Files
use java.nio.file.Path

pub fun readLines(path: Path) -> List<Text> {
    fix lines: List<Text> = []
    try {
        with reader = Files.newBufferedReader(path) {
            while true {
                fix line: Text? = reader.readLine()
                if line != null {
                    lines.add(line)
                }
                else {
                    stop
                }
            }
        }
    }
    handle (error: java.io.IOException) {
        sayLine("Could not read file")
        exit(1)
    }
    return lines
}
Input.java
package stats;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public final class Input {
    public static List<String> readLines(Path path) {
        final var lines = new ArrayList<String>();
        try (var reader = Files.newBufferedReader(path)) {
            String line;
            while ((line = reader.readLine()) != null) lines.add(line);
        } catch (IOException error) {
            System.out.println("Could not read file");
            System.exit(1);
        }
        return lines;
    }
}

with closes the reader, including on failure. Java I/O exceptions must be handled. A null line means the end of the file; a blank line is still a line.

Replace App.kalm:

App.kalm
space stats

use java.nio.file.Path

pub fun main(args: Array<Text>) -> Unit:
    if len(args) != 1:
        sayLine("Usage: kalm run -- <file>")
        exit(2)
        return

    fix result = summarize(readLines(Path.of(args[0])))
    sayLine(@"${result.lines()} lines / ${result.words()} words")
App.kalm
space stats

use java.nio.file.Path

pub fun main(args: Array<Text>) -> Unit {
    if len(args) != 1 {
        sayLine("Usage: kalm run -- <file>")
        exit(2)
        return
    }
    fix result = summarize(readLines(Path.of(args[0])))
    sayLine(@"${result.lines()} lines / ${result.words()} words")
}
App.java
package stats;

import java.nio.file.Path;

public final class App {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: kalm run -- <file>");
            System.exit(2);
            return;
        }
        final Statistics result = Summary.summarize(
            Input.readLines(Path.of(args[0]))
        );
        System.out.println(
            result.lines() + " lines / " + result.words() + " words"
        );
    }
}

Create notes.txt with two lines:

Text
a quiet
morning
Terminal
kalm.bat run -- notes.txt
Terminal
kalm.bat run -- notes.txt
Terminal
kalm run -- notes.txt
Terminal
kalm run -- notes.txt
Terminal
kalm run -- notes.txt
Terminal
kalm run -- notes.txt
Terminal
kalm run -- notes.txt
Terminal
kalm run -- notes.txt
Text
2 lines / 3 words

A missing or unreadable file prints an error and exits with status 1. An incorrect argument count prints usage and exits with status 2. The input file is read without changing it.

Save the complete program below as App.kalms in a folder of your choice. Each lesson replaces this file; the shell does not load neighboring project files. The script host does not forward command-line arguments. Edit the args array to try a different path or an empty input. Create notes.txt in the directory where you launch the command, containing a quiet on the first line and morning on the second.

App.kalm
use java.nio.file.Files
use java.nio.file.Path
use java.nio.file.Path

fun countWords(text: Text) -> Int:
    var words = 0
    for word in text.split("\\s+"):
        if not word.isBlank():
            words += 1

    return words

data Statistics(lines: Int, words: Int)

fun summarize(lines: List<Text>) -> Statistics:
    var words = 0
    for line in lines:
        words += countWords(line)

    return Statistics(len(lines), words)

fun readLines(path: Path) -> List<Text>:
    fix lines: List<Text> = []
    try:
        with reader = Files.newBufferedReader(path):
            while true:
                fix line: Text? = reader.readLine()
                if line != null:
                    lines.add(line)
                else:
                    stop
    handle (error: java.io.IOException):
        sayLine("Could not read file")
        exit(1)

    return lines

pub fun main(args: Array<Text>) -> Unit:
    if len(args) != 1:
        sayLine("Usage: kalm run -- <file>")
        exit(2)
        return

    fix result = summarize(readLines(Path.of(args[0])))
    sayLine(@"${result.lines()} lines / ${result.words()} words")
App.kalm
use java.nio.file.Files
use java.nio.file.Path
use java.nio.file.Path

fun countWords(text: Text) -> Int {
    var words = 0
    for word in text.split("\\s+") {
        if not word.isBlank() {
            words += 1
        }
    }
    return words
}

data Statistics(lines: Int, words: Int)

fun summarize(lines: List<Text>) -> Statistics {
    var words = 0
    for line in lines {
        words += countWords(line)
    }
    return Statistics(len(lines), words)
}

fun readLines(path: Path) -> List<Text> {
    fix lines: List<Text> = []
    try {
        with reader = Files.newBufferedReader(path) {
            while true {
                fix line: Text? = reader.readLine()
                if line != null {
                    lines.add(line)
                }
                else {
                    stop
                }
            }
        }
    }
    handle (error: java.io.IOException) {
        sayLine("Could not read file")
        exit(1)
    }
    return lines
}

pub fun main(args: Array<Text>) -> Unit {
    if len(args) != 1 {
        sayLine("Usage: kalm run -- <file>")
        exit(2)
        return
    }
    fix result = summarize(readLines(Path.of(args[0])))
    sayLine(@"${result.lines()} lines / ${result.words()} words")
}
App.kalms
fun countWords(text: Text) -> Int:
    var words = 0
    for word in text.split("\\s+"):
        if not word.isBlank():
            words += 1

    return words

data Statistics(lines: Int, words: Int)

fun summarize(lines: List<Text>) -> Statistics:
    var words = 0
    for line in lines:
        words += countWords(line)

    return Statistics(len(lines), words)

use java.nio.file.Files
use java.nio.file.Path

fun readLines(path: Path) -> List<Text>:
    fix lines: List<Text> = []
    try:
        with reader = Files.newBufferedReader(path):
            while true:
                fix line: Text? = reader.readLine()
                if line != null:
                    lines.add(line)
                else:
                    stop
    handle (error: java.io.IOException):
        sayLine("Could not read file")
        exit(1)

    return lines

use java.nio.file.Path

fix args: Array<Text> = arrayOf("notes.txt")
if len(args) != 1:
    sayLine("Usage: kalm run -- <file>")
    exit(2)
fix result = summarize(readLines(Path.of(args[0])))
sayLine(@"${result.lines()} lines / ${result.words()} words")
App.kalms
fun countWords(text: Text) -> Int {
    var words = 0
    for word in text.split("\\s+") {
        if not word.isBlank() {
            words += 1
        }
    }
    return words
}

data Statistics(lines: Int, words: Int)

fun summarize(lines: List<Text>) -> Statistics {
    var words = 0
    for line in lines {
        words += countWords(line)
    }
    return Statistics(len(lines), words)
}

use java.nio.file.Files
use java.nio.file.Path

fun readLines(path: Path) -> List<Text> {
    fix lines: List<Text> = []
    try {
        with reader = Files.newBufferedReader(path) {
            while true {
                fix line: Text? = reader.readLine()
                if line != null {
                    lines.add(line)
                }
                else {
                    stop
                }
            }
        }
    }
    handle (error: java.io.IOException) {
        sayLine("Could not read file")
        exit(1)
    }
    return lines
}

use java.nio.file.Path

fix args: Array<Text> = arrayOf("notes.txt")
if len(args) != 1 {
    sayLine("Usage: kalm run -- <file>")
    exit(2)
}
fix result = summarize(readLines(Path.of(args[0])))
sayLine(@"${result.lines()} lines / ${result.words()} words")
App.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

final class Counting {
    public static int countWords(String text) {
        int words = 0;
        for (final String word : text.split("\\s+"))
            if (!word.isBlank()) words += 1;
        return words;
    }
}

record Statistics(int lines, int words) {}

final class Summary {
    public static Statistics summarize(List<String> lines) {
        int words = 0;
        for (final String line : lines) words += Counting.countWords(line);
        return new Statistics(lines.size(), words);
    }
}

final class Input {
    public static List<String> readLines(Path path) {
        final var lines = new ArrayList<String>();
        try (var reader = Files.newBufferedReader(path)) {
            String line;
            while ((line = reader.readLine()) != null) lines.add(line);
        } catch (IOException error) {
            System.out.println("Could not read file");
            System.exit(1);
        }
        return lines;
    }
}

public final class App {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: kalm run -- <file>");
            System.exit(2);
            return;
        }
        final Statistics result = Summary.summarize(
            Input.readLines(Path.of(args[0]))
        );
        System.out.println(
            result.lines() + " lines / " + result.words() + " words"
        );
    }
}
Terminal
kalm.bat shell -f App.kalms
Terminal
kalm.bat shell -f App.kalms
Terminal
kalm shell -f App.kalms
Terminal
kalm shell -f App.kalms
Terminal
kalm shell -f App.kalms
Terminal
kalm shell -f App.kalms
Terminal
kalm shell -f App.kalms
Terminal
kalm shell -f App.kalms
Text
2 lines / 3 words