Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NIO2: Working with FileStore object


FileStore object represents a storage pool, device, partition, volume, concrete file system or other implementation specific means of file storage.

How to get an instance of Filestore object?
Files class provides 'getFileStore' method, it is used to get the file store where a file is located.

Example
FileStore fileStore = Files.getFileStore(Paths.get("/Users"));

App.java
package com.sample.app;

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {

public static void main(String args[]) throws IOException {

FileStore fileStore = Files.getFileStore(Paths.get("/Users"));

System.out.println("Name : " + fileStore.name());
System.out.println("Type : " + fileStore.type());
System.out.println("Total Space : " + fileStore.getTotalSpace() + " bytes");
System.out.println("Unallocated Space : " + fileStore.getUnallocatedSpace() + " bytes");
System.out.println("Unallocated Space : " + fileStore.getUsableSpace() + " bytes");

System.out.println("---------------------------\n");

}
}

Sample Output
Name : /dev/disk1s1
Type : apfs
Total Space : 499963174912 bytes
Unallocated Space : 354025279488 bytes
Unallocated Space : 347387514880 bytes
---------------------------

How to get all the FileStore objects available for given FileSystem?
FileSystem class provides 'getFileStores' method, it returns an iterableobject to iterate over the underlying file stores.

Example
FileSystem fileSystem = FileSystems.getFileSystem(URI.create("file:///"));
Iterable fileStores = fileSystem.getFileStores();



This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

NIO2: Working with FileStore object

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×