Java Program to Get the Size of Given File in Bytes, KiloBytes and MegaBytes


Size of a file is the amount of storage space occupied by that particular file on a particular storage device such as a hard drive. Size of a file is measured in terms of bytes. In this section, we will be discussing how to implement a java program to get the size of given file in bytes, kilobytes and megabytes.

A byte is the smallest unit of digital information. One byte is equal to eight bits.

  • 1 kilobyte (KB) = 1,024 bytes

  • 1 megabyte (MB) = 1,024 KB

  • gigabyte (GB) = 1,024 MB and

  • 1 terabyte (TB) = 1,024 GB.

The size of a file generally depends on the type of file and the amount of data it contains. Considering a text document as an example the size of file may only be a few kilobytes in size, while a high-resolution image or a video file can be several gigabytes in size.

File is a collection of information which may contain data such as text information, images, audio, videos, program code. These can be accessed by any software applications to perform actions like read, write, update, delete etc.

Syntax

Creating a file class object

File file = new File("file_path");

Create BufferedStramInout class object

BufferedInputStream input = new BufferedInputStream(new FileInputStream("filename"));

length() − This method returns the size of the file in bytes.

File file = new File("/example.txt");
long fileSize = file.length();

exists() − This method checks whether the file exists or not and returns boolean value.

File file = new File("/example.txt");
if (file.exists()) {
  System.out.println("File exists.");
}

read() − This method is used to read bytes from an input stream.

FileInputStream input = new FileInputStream(filePath)
byte[] buffer = new byte[10];
int bytesRead = input.read(buffer); 

size() − This method returns the size of the file in bytes.

Path path = Paths.get("/path/to/file");
long fileSize = Files.size(path);

Now, we will implement different java approaches to find the size of given file in bytes, kilobytes and megabytes.

Approach 1: Using java.io. Package

In this particular approach, we will use File class of java.io package and use different in built functions and get the Size of file.

Algorithm

  • Create a file object using File class.

  • Check whether file exists using exists() method and if file exists find the size using length() method.

  • Print the size in bytes, kilobytes, megabytes.

  • If file does not exist print the file not found.

Example

In this example, we have created a file object using the File class and we will check if the file exists using exists() function and if file exists then calculate the length of file using length() function to get the length of file and store in ‘sizebytes’ variable. We will print the size of file in bytes using ‘sizebytes’. For kilo bytes the size is ‘sizebytes’ divided by 1024 and for kilo bytes ‘sizebytes’ divided by 1024*1024.

import java.io.File;
public class Main{
   public static void main(String[] args) {
      File f = new File("C:/example.txt");
      if (f.exists()) {
         long sizebytes = f.length();
         System.out.println("File size in bytes is equal to : " + sizebytes);
         System.out.println("File size in kilobytes is equal to : " + (sizebytes/ 1024));
         System.out.println("File size in megabytes is equal to : " + (sizebytes / (1024 * 1024)));
      } else {
         System.out.println("File not found.");
      }
   }
}

Output

File size in bytes is equal to : 1048576 
File size in kilobytes is equal to : 1024
File size in megabytes is equal to : 1	

Approach 2: Using java.nio package

In this particular approach, we will use Path class of java.nio package and use different in built functions and get the Size of file.

Algorithm

  • Create a path object using get() method of Path class.

  • Find the size using size() method and print the size in bytes,kilobytes,megabytes

  • If you get an exception,print it

Example

In this example, we have created a file object using the Path class and we will use size() function to get the size of file and store in ‘sizebytes’ variable .We will print the size of file in bytes using ‘sizebytes’. For kilo bytes the size is ‘sizebytes’ divided by 1024 and for kilo bytes ‘sizebytes’ divided by 1024*1024.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
   public static void main(String[] args) {
      Path p = Paths.get("C:/example.txt");
      try {
         long sizebytes = Files.size(p);
         System.out.println("File size in bytes is equal to : " + sizebytes);
         System.out.println("File size in kilobytes is equal to : " + (sizebytes/ 1024));
         System.out.println("File size in megabytes is equal to : " + (sizebytes / (1024 * 1024)));
      } catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
      }
   }
}

Output

File size in bytes is equal to : 1048576 
File size in kilobytes is equal to : 1024
File size in megabytes is equal to : 1

Thus, in this article we have discussed different approaches in java to get size of given file in bytes, kilobytes and megabytes.

Updated on: 16-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements