Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 May 2015

How to get file size using Java

Following example shows how to get file size in bytes using file.length() method in Java.

length
public long length()
Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory. Where it is required to distinguish an I/O exception from the case that 0L is returned, or where several attributes of the same file are required at the same time, then the Files.readAttributes method may be used.
Returns:
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist.Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.
Throws:
SecurityException - If a security manager exists and its
SecurityManager.checkRead(java.lang.String) method denies read access to the file

Sample Program
FileSize.java
package com.javatutorialcorner.file;

import java.io.File;

public class FileSize {

 public static void main(String[] args) {
  String path = "/home/annamalai/workspace/File/jtc.txt";
  File jtcFile = new File(path);
  if (!jtcFile.exists() || !jtcFile.isFile()) {
   System.out.println("File doesn't exist");
  } else {
   System.out.println("File size in Bytes : " + jtcFile.length());
  }

 }

}


Result
The above code will produce the following output if file exist in specified directory.

File size in Bytes : 4

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to append content into existing file in JavaIn this tutorials we are going to see how to append content into existing file using FileWriter in Java. The constructor FileWriter(file,true) is used to append content into existing file.By default FileWriter(file) will re… Read More
  • Reading the file using FileInputStreamIn this tutorials we are going to see how to read file using FileInputStream. Create project called JavaMisc. Create package called com.javatutorialscorner.io Create java class called FileReader under com.javatutorialscorner.… Read More
  • Read File using BufferedReaderIn this tutorials we are going to see how to read file using BufferedReader. Create project called JavaMisc. Create package called com.javatutorialscorner.io Create java class called FileReaderExample under com.javat… Read More
  • Read File using BufferedInputStream In this tutorials we are going to see how to read file using BufferedInputStream. Create project called JavaMisc. Create package called com.javatutorialscorner.io  Create java class called FileReaderExample… Read More
  • How to Write File using BufferedWriterIn this tutorials we are going to see how to write file using BufferedWriter in Java. 1.Create project called JavaMisc. 2.Create package called com.javatutorialscorner.io Create java class called FileWriter under co… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to get file size using Java Rating: 5 Reviewed By: eHowToNow