Java Streams   «Prev  Next»

Lesson 9Course project, Character-mode Program
ObjectiveWrite a character-mode program that dumps a file named on the command line in ASCII, hex dump, or decimal dump.

Character Mode Program dumps File

In this course you are going to develop a program that lets you dump files to System.out. Since this program is going to be expanded in the next module, it is important to try to keep your code as general and adaptable as possible.

Character-mode program that dumps a file named on the command line in ASCII, hex dump, or decimal dump

Write a character-mode program that dumps a file named on the command line in ASCII, hex dump, or decimal dump using Java 8 Lambda Streams.
In this example, we'll create a Java program that takes a file name and a dump mode (ASCII, hex, or decimal) from the command line and displays the file content in the specified format using Java 8 Lambda Streams. We'll use the Files.lines() method to read the file content and process it with Lambda Streams:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;

public class FileDump {
   public static void main(String[] args) {
       if (args.length != 2) {
           System.err.println("Usage: java FileDump <file> <ascii|hex|decimal>");
           return;
       }

       Path filePath = Path.of(args[0]);
       String mode = args[1];

       try {
           dumpFile(filePath, mode);
       } catch (IOException e) {
           System.err.println("Failed to read the file: " + e.getMessage());
       }
   }

   public static void dumpFile(Path file, String mode) throws IOException {
       Files.lines(file, StandardCharsets.UTF_8)
               .flatMap(line -> line.chars().boxed())
               .forEach(ch -> {
                   switch (mode.toLowerCase()) {
                       case "ascii":
                           System.out.print((char) ch.intValue());
                           break;
                       case "hex":
                           System.out.printf("%02x ", ch);
                           break;
                       case "decimal":
                           System.out.printf("%03d ", ch);
                           break;
                       default:
                           throw new IllegalArgumentException("Invalid mode: " + mode);
                   }
               });

       System.out.println();
   }
}

In this example, the dumpFile method reads the file content line by line using the Files.lines() method, which returns a Stream<String>. The flatMap() operation is used to convert each line into a stream of characters, and the forEach() operation processes each character based on the specified dump mode (ASCII, hex, or decimal).
To compile and run the program, use the following commands in the terminal:
# Compile the program
javac FileDump.java

# Run the program with different modes
java FileDump input.txt ascii
java FileDump input.txt hex
java FileDump input.txt decimal

Replace input.txt with the file you want to dump, and choose the desired dump mode (ascii, hex, or decimal).

Read Filenames - Exercise

Click the Exercise link below to write a program whose main() method reads a series of filenames from the command line and then dumps the file in ASCII, hex dump, or decimal dump.
Read Filenames - Exercise