Home > DeveloperSection > Forums > How to determining File Length in java
Jonas Stuart
Jonas Stuart

Total Post:11

Points:77
Posted on    July-06-2016 3:22 AM

 Java Java 
Ratings:


 1 Reply(s)
 256  View(s)
Rate this:
Hi Guys 

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

Thanks


Manoj Bhatt
Manoj Bhatt

Total Post:153

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

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 Jul-06-2016 03:27:07 AM

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

Follow MindStick