HOW TO WRITE PROGRAM FOR FILE VIEWER UTILITY

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
 532  View(s)
Ratings:
Rate this:

Hi Guys 

Please help me .I want to write program for  file viewer utility.

thanks
  1. Jayden Bell

    Post:110

    Points:774
    Re: How to write program for file viewer utility

    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();

                                                    }

                                        }

                              }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!