Home Java Java-get-example Get File Size
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Get File Size

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

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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 5, 2008

Related Tutorials

Ask Questions?    Discuss: Get File Size  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com