Get File Size

In this Tutorial, we want to describe you a code that helps you in understanding Get File Size.


 
Ads

Get File Size

     

In this Tutorial, we want to describe you a code that helps you in understanding  Get File Size. For this we have a class name" Get File Size" Inside the main method  we have -

Input Stream Reader This is used to decode a byte code and convert the byte code to a character stream.
Buffered Reader This is used to read a character stream from a file.

We declare a string variable path assigned to memory. The System.out.println is used to print the path. In the Try block readline ( ) method provides you to read the file and store in  a path variable. Incase there is an exception occurred in a try block, the catch block followed by the try block to handle the exception. Otherwise we declare file variable name dir and assigned to a memory by passing path as argument. The dir variable call a list method return you a list of files  and store in a string array file list. We display the list of array file using for loop. Now in order to evaluate the size of file we use.isfile( ),returns you the size of file present in bytes by placing in the conditional operator if clause.

GetFileSize.java


import java.io.*;

public class GetFileSize {

  public static void main(String args[]) throws Exception {

  InputStreamReader streamReader = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(streamReader);
  String path = new String();
  System.out.println("Path : ");
  try {
  path = br.readLine();
  catch (Exception e) {
  System.out.println(e);
  }

  File dir = new File(path);
  String[] fileList = dir.list();
  File file;
  for (int i = 0; i < fileList.length; i++) {
  file = new File(fileList[i]);
  if (file.isFile()) {
  System.out.println(file.getName() +
  " : " + file.length() " Bytes");
  }
  }
  }
}

Output
Path :  .
build.xml : 3367 Bytes
manifest.mf : 85 Bytes
text.txt : 93 Bytes

Download code

Advertisements

Ads
Share on Google+Share on Google+

Get File Size

Posted on: November 5, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Get File Size  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads