---
title: "How to write program for file viewer utility"  
description: "How to write program for file viewer utility"  
author: "Manoj Bhatt"  
published: 2016-07-06  
updated: 2016-07-06  
canonical: https://www.mindstick.com/forum/34151/how-to-write-program-for-file-viewer-utility  
category: "java"  
tags: ["java", "java i/o"]  
reading_time: 4 minutes  

---

# How to write program for file viewer utility

Hi Guys \
Please help me .I want to write [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) for [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) viewer utility.\
thanks

## Replies

### Reply by Jayden Bell

**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.

**The program code for the file viewer utility**

```
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");

                                                System.exit(0);

                                    }

                                    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) {

                                                System.out.println(fe.getMessage());

                                                System.exit(0);

                                    } catch (IOException ioe) {

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

                                    } finally {

                                                try {

                                                            reader.close();

                                                            writer.close();

                                                } catch (Exception e) {

                                                            e.printStackTrace();

                                                }

                                    }

                          }

}
```


---

Original Source: https://www.mindstick.com/forum/34151/how-to-write-program-for-file-viewer-utility

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
