获取文件存储属性的两种方式

来源:互联网 发布:引力波 知乎 编辑:程序博客网 时间:2024/05/04 07:35
  • 获取系统中所有文件存储的属性
    FileSystem fs = FileSystems.getDefault();for (FileStore store : fs.getFileStores()) {try {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 (Exception e) {e.printStackTrace();}}

  • 通过文件,获取该文件存储的属性
    Path path = Paths.get("E:/1.sql");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);System.out.println("Available space: " +available_space);System.out.println("Is read only? " + is_read_only);} catch (Exception e) {e.printStackTrace();}


0 0