📜  在Java的Java类.nio.file.FileSystem

📅  最后修改于: 2022-05-13 01:55:01.668000             🧑  作者: Mango

在Java的Java类.nio.file.FileSystem

Java.nio.file.FileSystem类提供文件系统的接口。文件系统充当创建不同对象(如 Path、PathMatcher、 UserPrincipalLookupServiceWatchService)的工厂。这个对象的帮助来访问文件系统中的文件和其他对象。

语法:类声明

public abstract class FileSystem
extends Object
implements Closeable

这个类的构造函数如下:

ConstructorDescription
FileSystem()Creates a new object of FileSystem class

该类的方法如下:

MethodDescription
close()Closes this file system that has been opened.
getFileStores()Returns an iterable object, which is used to iterate over the underlying file stores.
getPath(String first, String… more)Converts given string to a Path or combines a given sequence of strings to form a Path.
getPathMatcher(String syntaxAndPattern)Returns a PathMatcher object which is used to perform match operations on the Path objects.
getRootDirectories()Returns an iterable object, which is used to iterate over the root directories.
getSeparator()Returns string representation of the name separator.
getUserPrincipalLookupService()Returns the UserPrincipalLookupService object for this file system. It is an optional operation.
isOpen()Used to check whether this file system is open or not.
isReadOnly()Used to check whether this file system allows read-only access to its file stores or not.
newWatchService()Returns a new WatchService object.
provider()Returns the provider that created this file system.
supportedFileAttributeViews()Returns the set consisting of the names of the file attribute views supported by this file system.

示例 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();
        }
    }
}


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();
        }
    }
}


输出:

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