Open In App

java.nio.file.FileStore Class in Java

Last Updated : 03 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Java.nio.file is a package in java that consists of the FileStore class. FileStore class is a class that provides methods for the purpose of performing some operations on file store.

  • FileStore is a class that extends Object from java.lang package. And few methods the FileStore class can inherit from java.lang.Object package is clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), toString(), wait().
  • getFileStore() is a method provided by FileStore class which is invoked to know where the file got stored in general it tells the location of the file where it got stored in the CD drive.
  • And FileStore also supports few or more classes like FileStoreAttributeView which provides a read-only or up view of a set of file store attributes.

Syntax: Class Declaration

public abstract class FileStore extends Object
{
abstract CheckResult( );
// Here an abstract method which 
// don't have usually a method definition part. 
} 

Note: Whenever you create an object for abstract class and tried to call it while compile time the compiler throws an error saying “the method is in-complete” this happens because Abstract is known as incomplete which means you cannot create an object for methods like this.

Constructors of FileStore Class is as follows: 

Constructor Description
FileStore() This constructor Initializes a new instance of this class.

Methods of FileStore Class are as follows:

Methods Description
 getAttribute(String attribute) This method reads the value of a file store attribute.
getFileStoreAttributeView(Class<V> type) This method returns a FileStoreAttributeView of the given type.
 getTotalSpace() This method returns the size, in bytes, of the file store.
getUnallocatedSpace() This method returns the number of unallocated bytes in the file store.
 getUsableSpace() This method returns the number of bytes available to this Java virtual machine on the file store.
isReadOnly() This method tells whether this file store is read-only.
 name() This method returns the name of this file store.
supportsFileAttributeView(Class<? extends FileAttributeView> type) This method tells whether this file store supports the file attributes identified by the given file attribute view.
 supportsFileAttributeView(String name) This method tells whether this file store supports the file attributes identified by the given file attribute view.
 type() This method returns the type of this file store.

Example 1:

Java




// Java Program to demonstrate FileStore Class
// with its methods
 
// Importing required libraries
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
 
// Main class
public class GFG {
 
    // Declaring and initializing variable
    static long Bytes = 1000;
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Creating an object of FileSystem class
        FileSystem fileSystem = FileSystems.getDefault();
 
        for (FileStore fileStore :
             fileSystem.getFileStores()) {
 
            // Here we use Bytes to
            // get the usable space in terms of bytes.
 
            // Here getUsableSpace method is used to
            // know the free space in the drive.
            // then it written back the value into
            // usableSpace variable
            long usableSpace
                = fileStore.getUsableSpace() / Bytes;
 
            // Here we use Bytes to
            // get the used space in terms of bytes.
 
            // Here we get the usedspace value by
            // subtracting the methods given below.
            long usedSpace = (fileStore.getTotalSpace()
                              - fileStore
 
                                    .getUnallocatedSpace())
                             / Bytes;
 
            // Readonly writes true or false based on
            // the mode the we file open.
            boolean readOnly = fileStore.isReadOnly();
 
            // Print and display the information
            // that the methods are allocated with
            System.out.println(
                "All information on the FileStore");
 
            // Print and display used and unused space
            System.out.println("Used Space : " + usedSpace);
            System.out.println("Un-Used Space : "
                               + usableSpace);
 
            // Print boolean true false whether it is read
            // only
            System.out.println("Is this read only : "
                               + readOnly);
        }
    }
}


 
 

Output:

 

 

Example 2:

 

Java




// Java Program to demonstrate FileStore Class
// with its methods
 
// Importing required libraries
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
 
// Main class
// FileStoreExample
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Creating an object of FileSystem class
        FileSystem fileSystem = FileSystems.getDefault();
 
        // Iterating for file storage using for each loop
        for (FileStore fileStore :
             fileSystem.getFileStores()) {
 
            // Here filestore() is used to know the
            // folder/drive name where the actual file is
            // getting stored
            String fileStoreName = fileStore.name();
 
            // This method returns the fileStore type
            String fileStoreType = fileStore.type();
 
            // Print and display commands
 
            // 1. information of file
            System.out.println(
                "All information on the FileStores\n\n");
 
            // 2. Name of a file stored
            System.out.println("File Store Name : "
                               + fileStoreName);
 
            // 3. Type of file stored
            System.out.println("File Store Type : "
                               + fileStoreType);
        }
    }
}


 
 

Output

 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads