files - File Systems

淳于鹏
2023-12-01

Two methods find out the rest of the information about the file system.

  • get the "default" file system using static FileSystems utility
  • call getFileSystem() on a Path object to get the file system that created that Path.
// files/FileSystemDemo.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.nio.file.*;

public class FileSystemDemo {
  static void show(String id, Object o) {
    System.out.println(id + ": " + o);
  }

  public static void main(String[] args) {
    System.out.println(System.getProperty("os.name"));
    FileSystem fsys = FileSystems.getDefault();
    for (FileStore fs : fsys.getFileStores()) {
      show("File Store", fs);
    }
    for (Path rd : fsys.getRootDirectories()) {
      show("Root Directory", rd);
    }
    show("Separator", fsys.getSeparator());
    show("UserPrincipalLookupService", fsys.getUserPrincipalLookupService());
    show("isOpen", fsys.isOpen());
    show("isReadOnly", fsys.isReadOnly());
    show("FileSystemProvider", fsys.provider());
    show("File Attribute Views", fsys.supportedFileAttributeViews());
  }
}
/* Output:
Windows 10
File Store: SSD (C:)
Root Directory: C:\
Root Directory: D:\
Separator: \
UserPrincipalLookupService:
sun.nio.fs.WindowsFileSystem$LookupService$1@15db9742
isOpen: true
isReadOnly: false
FileSystemProvider:
sun.nio.fs.WindowsFileSystemProvider@6d06d69c
File Attribute Views: [owner, dos, acl, basic, user]
*/

 

public abstract class FileStore
extends Object

Storage for files. A FileStore represents a storage pool, device, partition, volume, concrete file system or other implementation specific means of file storage. The FileStore for where a file is stored is obtained by invoking the getFileStore method, or all file stores can be enumerated by invoking the getFileStores method.

In addition to the methods defined by this class, a file store may support one or more FileStoreAttributeView classes that provide a read-only or updatable view of a set of file store attributes.

Since:

1.7

 

getUserPrincipalLookupService

public abstract UserPrincipalLookupService getUserPrincipalLookupService()

Returns the UserPrincipalLookupService for this file system (optional operation). The resulting lookup service may be used to lookup user or group names.

Usage Example: Suppose we want to make "joe" the owner of a file:

UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
     Files.setOwner(path, lookupService.lookupPrincipalByName("joe"));

Returns:

The UserPrincipalLookupService for this file system

Throws:

UnsupportedOperationException - If this FileSystem does not does have a lookup service

 

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/files/FileSystemDemo.java

3. https://docs.oracle.com/javase/8/docs/api/java/nio/file/FileStore.html

4. https://docs.oracle.com/javase/8/docs/api/java/nio/file/FileSystem.html#getUserPrincipalLookupService--

 类似资料: