当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java java.nio.file.FileSystem用法及代码示例


java.nio.file.FileSystem 类提供文件系统的接口。文件系统充当创建不同对象(如 Path、PathMatcher、UserPrincipalLookupService 和 WatchService)的工厂。该对象有助于访问文件系统中的文件和其他对象。

用法:类声明

public abstract class FileSystem
extends Object
implements Closeable

该类的构造函数如下:

构造函数 说明
FileSystem() 创建 FileSystem 类的新对象

该类的方法如下:

方法 说明
close() 关闭该已打开的文件系统。
getFileStores() 返回一个可迭代对象,用于迭代底层文件存储。
getPath(首先是字符串,字符串……更多) 将给定字符串转换为 Path 或组合给定字符串序列以形成 Path。
getPathMatcher(String syntaxAndPattern) 返回一个 PathMatcher 对象,用于对 Path 对象执行匹配操作。
getRootDirectories() 返回一个可迭代对象,用于迭代根目录。
getSeparator() 返回名称分隔符的字符串表示形式。
getUserPrincipalLookupService() 返回此文件系统的UserPrincipalLookupService对象。这是一个可选操作。
isOpen() 用于检查该文件系统是否打开。
isReadOnly() 用于检查该文件系统是否允许对其文件存储进行只读访问。
newWatchService() 返回一个新的WatchService对象。
provider() 返回创建此文件系统的提供者。
supportedFileAttributeViews() 返回由该文件系统支持的文件属性视图的名称组成的集合。

示例 1:

Java


// Java program to Create a new FileSystem object and
// print its file stores and root directories
// Importing required classes from java.nio package
// Importing input output classes
import java.io.*;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
            // Create a new File system Object
            FileSystem filesystem
                = FileSystems.getDefault();
            // Display messages only
            System.out.println(
                "File System created successfully");
            System.out.println(
                "Underlying file stores of this FileSystem :");
            // Print the Underlying file stores of this
            // FileSystem using for each loop
            for (FileStore store :
                 filesystem.getFileStores()) {
                System.out.println(store.toString());
            }
            // Display message only
            System.out.println(
                "Root directories of this FileSystem :");
            for (Path rootdir :
                 filesystem.getRootDirectories()) {
                // Print the Root directories of this
                // FileSystem using standard toString()
                // method
                System.out.println(rootdir.toString());
            }
        }
        // Catch block to handle exceptions
        catch (Exception e) {
            // Print the exception along with
            // line number where it occurred
            e.printStackTrace();
        }
    }
}

输出:

File System created successfully
Underlying file stores of this FileSystem :
/ (/dev/disk1s1)
/dev (devfs)
/private/var/vm (/dev/disk1s4)
/net (map -hosts)
/home (map auto_home)
Root directories of this FileSystem :
/

示例 2:

Java


// Java program to illustrate Working of FileSystem Class
// Via its Methods
// importing required classes from java.nio package
// Importing input output classes
import java.io.*;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
// Main class
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Try block to check for exceptions
        try {
            // Creating an object of FileSystem class in the
            // main() method using getDefault() method
            FileSystem filesystem
                = FileSystems.getDefault();
            // Display message only
            System.out.println(
                "FileSystem created successfully");
            // Checking if file system is open or close
            if (filesystem.isOpen())
                // Print statement
                System.out.println("File system is open");
            else
                // Print statement
                System.out.println("File system is close");
            // Check if file system is read-only
            if (filesystem.isReadOnly())
                // Print statement
                System.out.println(
                    "File system is Read-only");
            else
                // Print statement
                System.out.println(
                    "File system is not Read-only");
            // Now, print the name separator
            // using getSeparator() method
            System.out.println("Separator: "
                               + filesystem.getSeparator());
            // Print hash value of this file system
            // using hashCode() method
            System.out.println("Hashcode: "
                               + filesystem.hashCode());
            // Print provider of this file system
            System.out.println(
                "Provider: "
                + filesystem.provider().toString());
        }
        // Catch block to handle the exceptions
        catch (Exception e) {
            // Print the exception along with line number
            // using printStackTrace() method and
            // display it on the console
            e.printStackTrace();
        }
    }
}

输出:

FileSystem created successfully
File system is open
File system is not Read-only
Separator: /
Hashcode: 929338653
Provider: sun.nio.fs.MacOSXFileSystemProvider@4b1210ee 


相关用法


注:本文由纯净天空筛选整理自abhinavjain194大神的英文原创作品 java.nio.file.FileSystem class in java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。