HOW TO DETERMINING FILE LENGTH IN JAVA

Jonas Stuart

Total Post:27

Points:189
Posted by  Jonas Stuart
Java 
 752  View(s)
Ratings:
Rate this:
Hi Guys 

I am beginner in java programming .I want to know that how to write program for determining file length.

Thanks
  1. Manoj Bhatt

    Post:154

    Points:1086
    Re: How to determining File Length in java

    The program for determining the length of a file is given in here:

     public
    class FileLength {
                         public static void main(String[] args) {
                                 int count = 0;
                                 InputStream streamReader = null;
                                 if (args.length < 1) {
                                          System.out.println("Usage:java FileLength <filename>");
                                          System.exit(0);
                                 }
                                 try {
                                          streamReader= new FileInputStream(args[0]);
                                          while(streamReader.read() != -1) {
                                                    count++;
                                          }
                                          System.out.println(args[0]+ " length = " + count);
                                          streamReader.close();
                                 } catch (FileNotFoundException fe) {
                                          System.out.println("File" + args[0] + " was not found");
                                          System.exit(0);
                                 } catch(IOException ie) {
                                          System.out.println("Error reading file");
                                 } finally {
                                          try {
                                                    streamReader.close();
                                          } catch (Exception e) {
                                                    e.printStackTrace();
                                          }
                                 }
                         }
    }

      Modified On Apr-11-2018 04:48:12 AM

Answer

NEWSLETTER

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