java.nio.file.FileStore Class in Java
Last Updated :
03 Oct, 2022
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
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
public class GFG {
static long Bytes = 1000 ;
public static void main(String[] args) throws Exception
{
FileSystem fileSystem = FileSystems.getDefault();
for (FileStore fileStore :
fileSystem.getFileStores()) {
long usableSpace
= fileStore.getUsableSpace() / Bytes;
long usedSpace = (fileStore.getTotalSpace()
- fileStore
.getUnallocatedSpace())
/ Bytes;
boolean readOnly = fileStore.isReadOnly();
System.out.println(
"All information on the FileStore" );
System.out.println( "Used Space : " + usedSpace);
System.out.println( "Un-Used Space : "
+ usableSpace);
System.out.println( "Is this read only : "
+ readOnly);
}
}
}
|
Output:
Example 2:
Java
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
public class GFG {
public static void main(String[] args) throws Exception
{
FileSystem fileSystem = FileSystems.getDefault();
for (FileStore fileStore :
fileSystem.getFileStores()) {
String fileStoreName = fileStore.name();
String fileStoreType = fileStore.type();
System.out.println(
"All information on the FileStores\n\n" );
System.out.println( "File Store Name : "
+ fileStoreName);
System.out.println( "File Store Type : "
+ fileStoreType);
}
}
}
|
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...