Use the length() method in the File class. From the javadocs:

Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.

UPDATED Nowadays we should use the Files.size() method:

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

For the second part of the question, straight from File's javadocs:

  • getUsableSpace() Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname

  • getTotalSpace() Returns the size of the partition named by this abstract pathname

  • getFreeSpace() Returns the number of unallocated bytes in the partition named by this abstract path name

Answer from Óscar López on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › file size in java
File Size in Java | Baeldung
November 29, 2023 - To get the size of a file in megabytes (MB), kilobytes (KB), and gigabytes (GB) in Java, we need first to understand the relation between these units.
Discussions

java - Get total size of file in bytes - Stack Overflow
Possible Duplicate: java get file size efficiently More on stackoverflow.com
🌐 stackoverflow.com
What causes world file size to increase?
6 years, almost 7K karma. Not worth it to stick around and watch it go down in flames. Besides, I really didn't contribute much so I'll just lurk if I get bored. -- mass edited with https://redact.dev/ More on reddit.com
🌐 r/Minecraft
5
1
September 13, 2018
What is the download size of Minecraft java edition 1.14?
With the launcher, jar, libraries, and assets, roughly 525MB. This doesn't include any worlds, resource packs, datapacks, or additional versions that get installed. More on reddit.com
🌐 r/Minecraft
1
1
July 30, 2019
Why do people say that Python is slower than C++?

Because it is. Simple as that.

  • Interpretation is much slower than compilation. And no, interpretation is not simply "translating into machine language" like compilation is.

  • Pythons type system is inherently slow due to duck typing and its insane flexibility

  • The python reference implementation is explicitly not written for speed but for clarity

  • Manual memory management is somewhat faster than automatic one, especially in Python due to see above

More on reddit.com
🌐 r/AskProgramming
30
19
November 6, 2018
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-get-file-size
Java get file size | DigitalOcean
August 3, 2022 - Before we look into an example program to get file size, we have a sample pdf file with size 2969575 bytes. Java File length() method returns the file size in bytes. The return value is unspecified if this file denotes a directory. So before calling this method to get file size in java, make ...
🌐
Mkyong
mkyong.com › home › java › how to get file size in java
How to get file size in Java - Mkyong.com
July 23, 2020 - In Java, we can use `Files.size` to get the size of a file in bytes.
🌐
ZetCode
zetcode.com › java › filesize
Java file size - getting file size in Java
The length method of File returns the file size. This is the oldest API to find out the size of a file in Java.
🌐
Scaler
scaler.com › home › topics › java › getting file information using java io streams
How to get Java File Size - Scaler Topics
April 8, 2024 - The File class in java contains a length() method that returns the size of the file in bytes. To use this method, we first need to create an object of the File class by calling the File(String pathname) constructor.
🌐
GeeksforGeeks
geeksforgeeks.org › java › files-size-method-in-java-with-examples
Files size() method in Java with Examples - GeeksforGeeks
April 14, 2023 - size() method of java.nio.file.Files help us to get the size of a file (in bytes). This method returns the file size, in bytes by taking the path of the file as a parameter. The size may differ from the actual size on the file system due to ...
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › i/o › java get file size (with examples)
Java Get File Size (with Examples)
May 5, 2024 - Learn to get the size of a file or a directory in Java using IO classes File, Files and Common IO's FileUtils class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-get-the-size-of-given-file-in-bytes-kilobytes-and-megabytes
Java Program to Get the Size of Given File in Bytes, KiloBytes and MegaBytes - GeeksforGeeks
March 9, 2021 - The in-built File.length() method in java can be used to get file size in java. The length() function is a part of the File class in Java. This function returns the length of the file whose path was specified.
🌐
TutorialsPoint
tutorialspoint.com › javaexamples › file_size.htm
How to get a files size in bytes using Java
import java.io.File; public class FileSizeExample { public static void main(String[] args) { File file = new File("C:\\Users\\TutorialsPoint7\\Desktop\\abc.png"); if(file.exists()) { double bytes = file.length(); double kilobytes = (bytes / 1024); double megabytes = (kilobytes / 1024); double ...
🌐
Attacomsian
attacomsian.com › blog › java-get-file-size
How to get a file size in Java
December 7, 2019 - In Java 7+, you can use the Files.size() static method to get the size of a file in bytes.
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-get-file-size-using-Java.php
How to Get the Size of a File Using Java
Once you have done this, then you just need to use the length() function to get the size of the file. The Java length() function, when working with files, returns the size of the file in unit bytes.
🌐
MangoHost
mangohost.net › mangohost blog › howto: java get file size
Howto: Java Get File Size
August 3, 2025 - File size values are returned as long primitives, supporting files up to 9,223,372,036,854,775,807 bytes (approximately 9.2 exabytes). For files larger than this theoretical limit, you’d need specialized handling, though such scenarios are ...
🌐
Coderanch
coderanch.com › t › 404645 › java › Maximum-Java-File-Size
Maximum Java File Size (Beginning Java forum at Coderanch)
I don't think there's a (practical) limit on *.java file size, but there are effective limits on class sizes, and in particular, the amount of bytecode in an individual method (2^16 bytes, I believe.) It's not terribly hard to write a JSP which compiles down to a Java file which in turn won't ...
🌐
Baeldung
baeldung.com › home › java › java io › java – directory size
Java - Directory Size | Baeldung
January 5, 2024 - @Test public void whenGetFolderSizeRecursive_thenCorrect() { long expectedSize = 12607; File folder = new File("src/test/resources"); long size = getFolderSize(folder); assertEquals(expectedSize, size); } Note: listFiles() is used to list the contents of the given folder. Next – let’s see how to use Java 7 to get the folder size.
🌐
w3resource
w3resource.com › java-exercises › io › java-io-exercise-9.php
Java - Get file size in bytes, kb, mb
March 2, 2026 - import java.io.File; public class Exercise9 { public static void main(String[] args) { File file = new File("/home/students/test.txt"); if(file.exists()) { System.out.println(filesize_in_Bytes(file)); System.out.println(filesize_in_kiloBytes(file)); System.out.println(filesize_in_megaBytes(file)); } else System.out.println("File doesn't exist"); } private static String filesize_in_megaBytes(File file) { return (double) file.length()/(1024*1024)+" mb"; } private static String filesize_in_kiloBytes(File file) { return (double) file.length()/1024+" kb"; } private static String filesize_in_Bytes(File file) { return file.length()+" bytes"; } } ... Write a Java program to compute the size of a file in bytes and convert it to KB and MB with appropriate rounding.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-get-the-size-of-given-file-in-bytes-kilobytes-and-megabytes
Java Program to Get the Size of Given File in Bytes, KiloBytes and MegaBytes
June 26, 2024 - File size in bytes is equal to : 1048576 File size in kilobytes is equal to : 1024 File size in megabytes is equal to : 1 · In this particular approach, we will use Path class of java.nio package and use different inbuilt functions and get the Size of file.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-the-size-of-a-file-in-java
How to Get the Size of a File in Java? - GeeksforGeeks
February 7, 2024 - In this article, we will explore the most commonly used approaches to get the size of the file in Java.
🌐
Simplesolution
simplesolution.dev › java-get-file-size
Get File Size in Java - Simple Solution
File size: 31281 bytes 30.5478515625 KB 0.029831886291503906 MB 2.9132701456546783E-5 GB · import java.io.File; public class GetFileSizeExample2 { public static void main(String[] args) { String fileLocation = "D:\\SimpleSolution\\logo.png"; ...