Get Attributes of All File Stores - Java File Path IO

Java examples for File Path IO:File System

Description

Get Attributes of All File Stores

Demo Code

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

public class Main {
  public static void main(String[] args) {
    // get information for all the stores in the default file system
    FileSystem fs = FileSystems.getDefault();
    for (FileStore store : fs.getFileStores()) {
      try {/* w  w w  .  j a  va  2s .co m*/
        long total_space = store.getTotalSpace() / 1024;
        long used_space = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024;
        long available_space = store.getUsableSpace() / 1024;
        boolean is_read_only = store.isReadOnly();

        System.out.println("--- " + store.name() + " --- " + store.type());
        System.out.println("Total space: " + total_space);
        System.out.println("Used space: " + used_space);
        System.out.println("Available space: " + available_space);
        System.out.println("Is read only? " + is_read_only);
      } catch (IOException e) {
        System.err.println(e);
      }
    }
  }
}

Result


Related Tutorials