Sunday, 16 June 2019

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<FileStore> fileStores = fileSystem.getFileStores();


App.java
package com.sample.app;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;

public class App {

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

  FileSystem fileSystem = FileSystems.getFileSystem(URI.create("file:///"));

  Iterable<FileStore> fileStores = fileSystem.getFileStores();

  for (FileStore fileStore : fileStores) {
   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 : 354038947840 bytes
Unallocated Space : 347401183232 bytes
---------------------------

Name : devfs
Type : devfs
Total Space : 192000 bytes
Unallocated Space : 0 bytes
Unallocated Space : 0 bytes
---------------------------

Name : /dev/disk1s4
Type : apfs
Total Space : 499963174912 bytes
Unallocated Space : 494594437120 bytes
Unallocated Space : 347401183232 bytes
---------------------------

Name : map -hosts
Type : autofs
Total Space : 0 bytes
Unallocated Space : 0 bytes
Unallocated Space : 0 bytes
---------------------------

Name : map auto_home
Type : autofs
Total Space : 0 bytes
Unallocated Space : 0 bytes
Unallocated Space : 0 bytes
---------------------------

Name : /dev/disk2s1
Type : hfs
Total Space : 77553664 bytes
Unallocated Space : 45424640 bytes
Unallocated Space : 45424640 bytes
---------------------------

Name : /dev/disk1s3
Type : apfs
Total Space : 499963174912 bytes
Unallocated Space : 498924687360 bytes
Unallocated Space : 347401183232 bytes
---------------------------





Previous                                                 Next                                                 Home

No comments:

Post a Comment