集册 Java实例教程 获取文件存储信息

获取文件存储信息

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

399
文件存储机制可以是设备,例如C驱动器,驱动器的分区,卷或其他组织文件系统空间的方式。
/*NowJava.com 提供*/

import java.io.IOException;

import java.nio.file.FileStore;

import java.nio.file.FileSystem;

import java.nio.file.FileSystems;

import java.text.NumberFormat;


public class Main {

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

    long kiloByte = 1024;

    String format = "%-16s %-20s %-8s %-8s %12s %12s %12s\n";

    System.out.printf(format, "Name", "Filesystem", "Type", "Readonly",

        "Size(KB)", "Used(KB)", "Available(KB)");

    FileSystem fileSystem = FileSystems.getDefault();

    for (FileStore fileStore : fileSystem.getFileStores()) {

      try {

        long totalSpace = fileStore.getTotalSpace() / kiloByte;

        long usedSpace = (fileStore.getTotalSpace() - fileStore/*时 代      J a v a   公   众 号 - nowjava.com*/

            .getUnallocatedSpace()) / kiloByte;

        long usableSpace = fileStore.getUsableSpace() / kiloByte;

        String name = fileStore.name();

        String type = fileStore.type();

        boolean readOnly = fileStore.isReadOnly();


        NumberFormat numberFormat = NumberFormat
展开阅读全文