當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。