The Wayback Machine - https://web.archive.org/web/20240910204813/https://www.geeksforgeeks.org/files-size-method-in-java-with-examples/
Open In App

Files size() method in Java with Examples

Last Updated : 14 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

  1. IOException if an I/O error occurs.
  2. 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)



Similar Reads

Files deleteIfExists() method in Java with Examples
The deleteIfExists() method of java.nio.file.Files help us to delete a file if the file exists at the path. we pass the path of the file as a parameter to this method. This method will return true if the file was deleted by this method; false if the file could not be deleted because it did not exist. If the file is a symbolic link then the symbolic
3 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 the file is a directory then this method will delete
3 min read
Files getFileStore() method in Java with Examples
getFileStore() method of java.nio.file.Files help us to return the FileStore object which represents the file store where a file is located. Once you get a reference to the FileStore you can apply filestore type of operation to get information of filestore. Syntax: public static FileStore getFileStore(Path path) throws IOException Parameters: This
2 min read
Files isHidden() method in Java with Examples
isHidden() method of java.nio.file.Files help us to check whether or not a file is hidden. This method return true if the file is considered hidden else it returns false. The exact definition of hidden is platform or provider dependent. On UNIX, for example, a file is considered to be hidden if its name begins with a period character ('.'). On Wind
2 min read
Files isReadable() method in Java with Examples
isReadable() method of java.nio.file.Files help us to check whether Java virtual machine has appropriate privileges that would allow it to open this file for reading or not. This method test file is readable or not. This method checks that a file exists or not and if file exists then it is readable or not. This method returns true if the file exist
2 min read
Files isWritable() method in Java with Examples
isWritable() method of java.nio.file.Files help us to check whether Java virtual machine has appropriate privileges that would allow it to open this file for writing or not. It means this method test file is writable or not. This method checks that a file exists or not and if file exists then it is writable or not. This method returns true if the f
2 min read
Files isExecutable() method in Java with Examples
isExecutable() method of java.nio.file.Files help us to check whether a file is executable. This method checks that a file exists on this path or not and if it exists then Java virtual machine has appropriate privileges to execute the file or not. The semantics may differ when checking access to a directory. For example, on UNIX systems, checking f
2 min read
mismatch() Method of java.nio.file.Files Utility Class with Examples
mismatch() method is newly added in java jdk-12 in the java.nio.file.Files class. This method is mainly used to compare each byte of data of two files from start and return the point at which the bit differs. If the bit doesn't differ at any point then it returns "-1L" indicating that files are the same and exact copies of each other. This method m
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 class provides 3 types of copy() method. Using copy(I
5 min read
Files createTempDirectory() Method in Java with Examples
The createTempDirectory() method of java.nio.file.Files Class is used to create a new directory using the given prefix and attributes. The given prefix acts as the name of the formed directory, may be null. The directory is set with given attributes. Based on the type of arguments passed, the Files class provides 2 types of createTempDirectory() me
4 min read
Dictionary size() Method in Java with Examples
The size() Method of Dictionary class in Java is used to know the size of the dictionary or the number of distinct keys present in the dictionary. Syntax: DICTIONARY.size() Parameters: The method does not accept any parameters. Return Value: The method returns the number of keys present in the dictionary. Below programs illustrate the size() Method
2 min read
TreeMap size() Method in Java with Examples
size() method of TreeMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. --> java.util package --> TreeMap class --> size() Method Syntax: Tree_Map.size() Return Value: The size of the map which also means the number of key-value pairs present in the map. Example 1: Mapping
2 min read
ArrayList size() method in Java with Examples
The size() method of java.util.ArrayList class is used to get the number of elements in this list. Syntax: public int size() Returns Value: This method returns the number of elements in this list. Below are the examples to illustrate the size() method. Example 1: // Java program to demonstrate // size() method // for Integer value import java.util.
2 min read
AbstractCollection size() Method in Java with Examples
The size() method of Java AbstractCollection is used to get the size of the Collection or the number of elements present in the Collection.Syntax: AbstractCollection.size() Parameters: The method does not take any parameter.Return Value: The method returns the size or the number of elements present in the collection.Below programs illustrates the u
2 min read
AbstractMap size() Method in Java with Examples
The AbstractMap.size() method of AbstractMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. Syntax: AbstractMap.size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the map which also means the number of key-value pairs present in
2 min read
List size() method in Java with Examples
The size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container. Syntax: public int size() Parameters: This method does not take any parameters. Return Value: This method returns the number of elements in this list. Illus
2 min read
DelayQueue size() method in Java with Examples
The size() method of DelayQueue class in Java is used to return the number of elements present in the given queue. If the number of elements is greater than Integer.MAX_VALUE then Integer.MAX_VALUE is returned as the size of this DelayQueue.Syntax: public int size() Parameters: It does not takes in any parameter.Return Value: It returns an integer
1 min read
BitSet size() Method in Java with Examples
The size() Method of BitSet class in Java is used to know the size of this BitSet. This size is equal to the number of bits, each element has occupied in the BitSet. The maximum element in the set is the size - the first element Syntax: BitSet.hashCode() Parameters: The method does not accept any parameters. Return Value: The method returns the num
2 min read
ConcurrentSkipListMap size() method in Java with Examples
The size() method of java.util.concurrent.ConcurrentSkipListMap is an in-built function in Java which that is it returns the number of keys mapped to this map. The size() method is not a constant time operation. In case the map contains more than Integer.MAX_VALUE elements, the maximum value of the map is returned. Syntax: public int size() Paramet
2 min read
CharArrayWriter size() method in Java with examples
The size() method of the CharArrayWriter class in Java is used to get the current size of the buffer. Syntax: public int size() Parameters: This method does not accept any parameter.Return Value: This method returns an integer value which signifies the current size of the buffer. Below program illustrate the above method: Program 1: Java Code // Ja
2 min read
SortedSet size() method in Java with Examples
The size() method of SortedSet interface is used to get the size of the SortedSet or the number of elements present in the SortedSet. Syntax: int size() Parameters: This method does not takes any parameter. Return Value: The method returns the size or the number of elements present in the SortedSet. Note: The size() method in SortedSet is inherited
1 min read
SortedMap size() method in Java with Examples
The size() method of SortedMap interface in Java is used to get the size of the SortedMap which refers to the number of the key-value pair or mappings in the SortedMap. Syntax: int size() Parameters: The method does not take any parameters. Return Value: The method returns the size of the SortedMap which also means the number of key-value pairs pre
2 min read
BlockingDeque size() method in Java with Examples
The size() method of BlockingDeque returns the current size of the Deque container. On calling the function the number of elements in the Deque container is returned. If the container is capacity restricted, then also it returns the number of elements which are present in the container at the time of function call. Syntax: public int size() Returns
2 min read
CompositeName size() method in Java with Examples
The size() method of a javax.naming.CompositeName class is used to return the size of the composite name object which is equal to the number of components in this composite name. Syntax: public int size() Parameters: This method accepts nothing. Return value: This method returns nonnegative number of components in this composite name. Below program
1 min read
CompoundName size() method in Java with Examples
The size() method of a javax.naming.CompoundName class is used to return the size of the compound name object which is equal to the number of components in this compound name. Syntax: public int size() Parameters: This method accepts nothing. Return value: This method returns nonnegative number of components in this compound name. Below programs il
2 min read
ByteArrayOutputStream size() method in Java with Examples
The size() method of ByteArrayOutputStream class in Java is used to obtain the current size of the buffer. This buffer is accumulated inside the ByteArrayOutputStream. This method returns the size of the current buffer as an integer type. Syntax: public int size() Parameters: This method does not accept any parameter. Return value: This method retu
2 min read
Map size() Method in Java With Examples
Map size() method in Java is used to get the total number entries i.e, key-value pair. So this method is useful when you want total entries present on the map. If the map contains more than Integer.MAX_VALUE elements returnInteger.MAX_VALUE Syntax: int size(); Parameter: This method does not take any parameter. Return value: This method returns the
1 min read
ZipFile size() function in Java with examples
The size() function is a part of java.util.zip package. The function returns the number of entries of the zip file. Function Signature: public int size() Syntax: zip_file.size(); Parameters: The function does not require any parameters Return value: The function returns a Integer, which is the number of entries of the zip file Exceptions: The funct
2 min read
HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
HashMap is a data structure that uses a hash function to map identifying values, known as keys, to their associated values. It contains "key-value" pairs and allows retrieving value by key. The most impressive feature is it's fast lookup of elements especially for large no. of elements. It is not synchronized by default but we can make it so by cal
6 min read
JAR files in Java
A JAR (Java Archive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform. In simple words, a JAR file is a file that contains a compressed version of .class files, audio files, image fi
3 min read
Practice Tags :