Get Attributes of the File Store in Which a File Resides - Java File Path IO

Java examples for File Path IO:File System

Description

Get Attributes of the File Store in Which a File Resides

Demo Code

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

public class Main {
  public static void main(String[] args) {

    // get information about a file store where a particular file resides
    Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt");
    try {/*from   www .  ja  v  a 2 s  . c  o  m*/
      FileStore store = Files.getFileStore(path);

      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