Home > DeveloperSection > Forums > How to write program for file viewer utility
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    July-06-2016 3:31 AM

 Java Java  Java I/O 
Ratings:


 1 Reply(s)
 254  View(s)
Rate this:

Hi Guys 

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

thanks


Jayden Bell
Jayden Bell

Total Post:105

Points:739
Posted on    July-06-2016 3:46 AM

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

                                                }

                                    }

                          }

}


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

Follow MindStick