集册 Java实例教程 获取文件所在的文件存储区的属性

获取文件所在的文件存储区的属性

欢马劈雪     最近更新时间:2020-01-02 10:19:05

443
获取文件所在的文件存储的属性
/** 来自 N o w J a v a . c o m - 时  代  Java**/

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 {

      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);
      /*
      n o w j a v a . c o m
      */

      
展开阅读全文