Line Filters in Fortress
Here’s the translation of the Go line filter program to Java, along with explanations in Markdown format suitable for Hugo:
A line filter is a common type of program that reads input on stdin, processes it, and then prints some derived result to stdout. grep
and sed
are common line filters.
Here’s an example line filter in Java that writes a capitalized version of all input text. You can use this pattern to write your own Java line filters.
To try out our line filter, first make a file with a few lowercase lines.
Then use the line filter to get uppercase lines.
In this Java version, we use BufferedReader
to read input line by line from System.in
. The readLine()
method returns null
when it reaches the end of the input, which is how we detect when to stop reading.
We use a try-catch block to handle potential IOException
s that might occur during reading. If an error occurs, we print it to System.err
and exit with a non-zero status code.
The toUpperCase()
method is used to convert each line to uppercase, and System.out.println()
is used to print the result.
Remember to compile the Java file before running it:
Then you can use it in a pipeline as shown in the example above.