Files size() method in Java with Examples Last Updated : 14 Apr, 2023 Comments Improve Suggest changes Like Article Like Report 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 compression, support for sparse files, or other reasons. The size of files that are not regular files is implementation-specific and therefore unspecified. Syntax: public static long size(Path path) throws IOException Parameters: This method accepts a parameter path which is the path to the file. Return value: This method returns the file size, in bytes. Exception: This method will throw following exceptions: IOException if an I/O error occurs.SecurityException in the case of the default provider, and a security manager is installed, its checkRead method denies read access to the file. Below programs illustrate size(Path) method: Program 1: Java // Java program to demonstrate // Files.size() method import java.io.IOException; import java.nio.file.*; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("D:\\GIT_EWS_PROJECTS\\logger" + "\\src\\logger" + "\\GFG.java"); // get File Size long result; result = Files.size(path); System.out.println("File " + path + " Size = " + result + " bytes"); } } Output: Program 2: Java // Java program to demonstrate // Files.size() method import java.io.IOException; import java.nio.file.*; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = Paths.get("D:\\User Aman\\" + "Documents\\MobaXterm\\" + "\\ArrayList.docx"); // get File Size long result; result = Files.size(path); System.out.println("File " + path + " Size = " + result + " bytes"); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Files.html#size?(java.nio.file.Path) Create Quiz Comment A AmanSingh2210 Follow 0 Improve A AmanSingh2210 Follow 0 Improve Article Tags : Java Java-Functions java.nio.file package Java-Files Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like