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) Comment More infoAdvertise with us Next Article Files size() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions java.nio.file package Java-Files Practice Tags : Java Similar Reads File mkdir() method in Java with examples The mkdir() method is a part of File class. The mkdir() function is used to create a new directory denoted by the abstract pathname. The function returns true if directory is created else returns false. Function Signature: public boolean mkdir() Syntax: file.mkdir() Parameters: This method do not ac 2 min read FileStore name() method in Java with Examples The name() method of a FileStore class is used to return the name of this file store as a String. The format of the name is typically the name of the storage pool or volume. The string returned by this method may differ from the string returned by the toString() method. Syntax: public abstract Strin 2 min read FileStore type() method in Java with Examples The type() method of a FileStore class is used to return the type of this file store and the format of the returned string highly implementation-specific. Syntax:Â public abstract String type()Parameters: This method accepts nothing. Return value: This method returns a string representing the type o 2 min read Files copy() Method in Java with Examples The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. Methods: Based on the type of arguments passed, the Files cla 5 min read URL getFile() method in Java with Examples The getFile() function is a part of URL class. The function getFile() returns the file name of a specified URL. The getFile() function returns the path and the query of the URL. Function Signature: public String getFile() Syntax: url.getFile() Parameter: This function does not require any parameter 2 min read Files delete() method in Java with Examples The delete() method of java.nio.file.Files help us to delete a file located at the path passed as a parameter. This method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted. If th 3 min read File length() method in Java with Examples The length() function is a part of File class in Java . This function returns the length of the file denoted by the this abstract pathname was length.The function returns long value which represents the number of bytes else returns 0L if the file does not exists or if an exception occurs. Function s 2 min read File isFile() method in Java with Examples The isFile() function is a part of File class in Java. This function determines whether the is a file or Directory denoted by the abstract filename is File or not. The function returns true if the abstract file path is File else returns false. Function signature: public boolean isFile() Syntax: file 2 min read File delete() method in Java with Examples The delete() function is a part of File class in Java . This function deletes an existing file or directory. If the file is deleted then the function returns true else returns false Function signature: public boolean delete() Syntax: boolean var = file.delete(); Parameters: This method does not acce 2 min read File canRead() method in Java with Examples The canRead()function is a part of the File class in Java. This function determines whether the program can read the file denoted by the abstract pathname. The function returns true if the abstract file path exists and the application is allowed to read the file.Function signature: public boolean ca 2 min read Like