Home > DeveloperSection > Blogs > Java I/O: File Viewer Utility

Java I/O: File Viewer Utility

Java Java 
1 Comment(s)
 371  View(s)
Rate this:

Java I/O: File Viewer Utility

Let’s write a utility called File Viewer that accepts the name of a character file on the command line and displays its contents to the user console.

Program Code       

The complete program for the file viewer is given here:

import java.io.*;


public class FileView {

                     public static void main(String[] args) {

                             int numberRead = 0;

                             FileReader reader = null;

                             PrintWriter writer = null;

                             char buffer[] = new char[512];

                             if (args.length < 1) {

                                      System.out.println("Usage: java FileView filename");



                             try {

                                      reader = new FileReader(args[0]);

                                      writer = new PrintWriter(System.out);

                                      while ((numberRead = reader.read(buffer)) != -1) {

                                                writer.write(buffer, 0, numberRead);


                             } catch (FileNotFoundException fe) {



                             } catch (IOException ioe) {

                                      System.out.println("Error reading/writing file");

                             } finally {

                                      try {



                                      } catch (Exception e) {








·         The file viewer accepts one command-line argument; if it is missing, the user is informed about the proper usage of the file viewer utility. After accepting the correct number of arguments from the user, the program opens the specified file by instantiating the FileReader class:

                       reader = new FileReader(args[0]);

The program opens an output stream on the user console using the following statement:

                        writer = new PrintWriter(System.out);

·         Note that System.out refers to the output stream where the system by default outputs its contents. We create an instance of the PrintWriter class on top of this output stream.

·         The PrintWriter class provides methods for writing primitive data and user-defined types. It is very useful in outputting text-based data in a formatted way. For instance, it can write int, long, and other primitive data types as text rather than as their byte values.

 The overloaded print method accepts a boolean, char, int, float, double, long, or other as an argument and prints its value in the text format. These methods take the form shown here:

·         public void print(int i)

·         public void print(long l)

·         public void print(float f)

·         public void print(double d)

·         ...


The class also provides the println version of all these methods, which prints a newline character immediately following the specified data type. Thus, by using this class, we are not required to convert primitive data types to text for printing to the console or any writer instance.

The PrintWriter class also contains a powerful format method that allows us to print a list of arguments of various data types in a specified format string and their locale. For those who are more conversant in C language programming, the PrintWriter class also provides a method called printf (same as the C language method) that offers the same functionality as the format method.

Next, we set up a loop for reading and writing files until the end-of-file (EOF) condition is reached on the input stream:

while ((numberRead = reader.read(buffer)) != -1) {

writer.write(buffer, 0, numberRead);




·         The read method of the FileReader class reads the file contents into the buffer specified in its argument. It normally returns the number of characters read and returns –1 on the EOF condition. The number of characters read usually equals the buffer length, except for the last read operation, which returns a lesser number because the EOF condition is reached.

·         The write method of the PrintWriter class writes the buffer contents to the output stream. The second parameter of the write method specifies the offset in the buffer from where the writing should begin. The third parameter of the write method indicates the number of characters to write from the buffer.

After the while loop terminates, we close both the files by calling the close method on the two instances in the finally block.

As in the earlier cases, all operations on files are enclosed in a try-catch block, and we provide appropriate exception handlers for the checked exceptions.

Running the program:

To run the program, you would use the following command line:

C:\360\io\>java FileView filename

The program dumps the contents of the file specified by filename to the user console. The file specified by the filename parameter must be available in the current working folder from where the File Viewer utility is executed. Alternatively, we could specify a relative or absolute path to the file.

Nice post

By Jayden Bell on   3 months ago
Thanks David for this post.

Don't want to miss updates? Please click the below button!

Follow MindStick