java.nio.file.FileSystems 類充當創建新文件係統的工廠。此類提供了創建文件係統的方法。該文件係統充當創建不同對象(如 Path、PathMatcher、UserPrincipalLookupService 和 WatchService)的工廠。該對象有助於訪問文件係統中的文件和其他對象。
用法:類聲明
public final class FileSystems extends Object
該類的方法如下:
方法 | 說明 |
---|---|
getDefault() | 此方法返回一個新的默認文件係統。 |
getFileSystem(URI uri) | 此方法返回對現有文件係統的引用。 |
newFileSystem(Path path, ClassLoader loader) | 該方法用於構造一個新的FileSystem來訪問該文件係統的文件內容。 |
newFileSystem(URI uri, Map<String,?> env) | 此方法用於使用給定的 URI 創建新的文件係統 |
newFileSystem(URI uri, Map<String,?> env, ClassLoader loader) | 此方法用於使用給定的 URI 創建新的文件係統 |
示例 1:
Java
// Java Program to illustrate FileSystems Class by
// creating a new file system using getDefault() method and
// printing its file stores and root directories
// Importing classes from java.nio package
// for network linking
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 by
// creating object of FileSystem class
// using getDefault() method
FileSystem filesystem
= FileSystems.getDefault();
// Display commands only
System.out.println(
"File System created successfully");
System.out.println(
"Underlying file stores of this FileSystem :");
// Printing the underlying file stores of this
// FileSystem using for each loop
for (FileStore store :
filesystem.getFileStores()) {
// Print statement
System.out.println(store.toString());
}
// Display message only
System.out.println(
"Root directories of this FileSystem :");
// Printing the root directories of this
// FileSystem using for each loop
for (Path rootdir :
filesystem.getRootDirectories()) {
// Print statement
System.out.println(rootdir.toString());
}
}
// Catch block to handle the exceptions
catch (Exception e) {
// Print the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
}
輸出:
示例 2:
Java
// Java Program to illustrate FileSystems Class by
// creating new file system using newFileSystem() method
// Importing URI class from java.net package
import java.net.URI;
// Importing required file classes from java.nio package
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
// Importing Map and HashMap classes from java.util package
import java.util.HashMap;
import java.util.Map;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Try block to check for exceptions
try {
// Creating object of Map class
// Declaring object of string types
Map<String, String> env = new HashMap<>();
// Getting path of zip file
Path zipPath = Paths.get("ZipFile.zip");
// Creating URI from zip path received
URI Uri
= new URI("jar:file",
zipPath.toUri().getPath(), null);
// Create new file system from uri
FileSystem filesystem
= FileSystems.newFileSystem(Uri, env);
// Display message for better readability
System.out.println(
"FileSystem created successfully");
// Checking if file system is open or not
// using isOpen() method
if (filesystem.isOpen())
// Print statement
System.out.println("File system is open");
else
// Print statement
System.out.println("File system is close");
}
// Catch block to handle the exceptions
catch (Exception e) {
// Print the exception with line number
// using the printStack() method
e.printStackTrace();
}
}
}
輸出:
File System created successfully File system is open
相關用法
- Java java.nio.file.FileSystem用法及代碼示例
- Java java.nio.file.FileStore用法及代碼示例
- Java java.nio.file.LinkPermission用法及代碼示例
- Java java.nio.file.attribute.AclEntry用法及代碼示例
- Java java.nio.file.spi.FileTypeDetector用法及代碼示例
- Java java.nio.file.attribute.FileTime用法及代碼示例
- Java java.nio.file.SimpleFileVisitor用法及代碼示例
- Java java.nio.file.Paths用法及代碼示例
- Java java.nio.ByteBuffer用法及代碼示例
- Java java.nio.IntBuffer用法及代碼示例
- Java java.nio.FloatBuffer用法及代碼示例
- Java java.nio.ShortBuffer用法及代碼示例
- Java java.nio.DoubleBuffer用法及代碼示例
- Java java.nio.channels.spi.SelectorProvider用法及代碼示例
- Java java.nio.charset.CoderResult用法及代碼示例
- Java java.nio.charset.CodingErrorAction用法及代碼示例
- Java java.nio.channels.spi.AsynchronousChannelProvider用法及代碼示例
- Java java.nio.charset.CharsetEncoder用法及代碼示例
- Java java.nio.charset.Charset用法及代碼示例
- Java java.nio.LongBuffer用法及代碼示例
- Java java.nio.channels.Selector用法及代碼示例
- Java java.nio.ByteOrder用法及代碼示例
- Java java.nio.CharBuffer用法及代碼示例
- Java java.nio.Buffer用法及代碼示例
- Java java.net.SocketException用法及代碼示例
注:本文由純淨天空篩選整理自abhinavjain194大神的英文原創作品 java.nio.file.FileSystems Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。