如何在 Java 中获取文件存储类型?
在 Java 中,java.nio.file.FileStore 类表示存储池、设备、分区、卷或其他实现特定的文件存储方式。FileStore 类提供方法来查询有关存储设备的信息,例如其总空间和可用空间、文件系统类型,以及它是否支持某些功能,如文件属性或符号链接。
FileStore 类的 type() 方法返回一个表示文件存储类型的字符串。文件存储类型是一个字符串,用于标识文件存储使用的文件系统类型。文件系统类型的示例包括 Windows NT 文件系统的“NTFS”、Linux 使用的第四扩展文件系统的“ext4”以及 macOS 使用的分层文件系统的“HFS+”。
让我们开始吧!
例如
假设源文件为
" C:/Users/SAMPLE /Desktop/Tutorial/Program/example.txt”
执行获取存储类型操作后,结果将是
文件存储类型:NTFS
算法
步骤 1:声明并初始化源文件路径。
步骤 2:创建 FileStore 对象。
步骤 3:使用 type() 方法获取文件的文件存储类型。
步骤 4:打印结果。
语法
getFileStore():它是在 java.nio.file.Path 类中定义的方法。它用于获取表示文件存储或文件所在分区的 FileStore 对象。
多种方法
我们提供了不同方法的解决方案。
使用静态方法
使用用户自定义方法
让我们逐一查看程序及其输出。
方法 1:使用静态方法
在此方法中,我们将分配默认文件系统。然后,根据算法,我们将检查 Java 中是否打开了特定文件系统。
在此方法中,将分配路径位置。然后,根据算法,我们将了解 Java 中的文件存储类型。
示例
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main
{
//main method
public static void main(String[] args)
{
//getting the file path
Path path =
Paths.get("C:/Users/SAMPLE/Desktop/Tutorial/Program/example.txt");
//try block
try {
//getting the FileStore object
FileStore store = Files.getFileStore(path);
//getting the file store type
String type = store.type();
//print the result
System.out.println("File store type: " + type);
//catch block
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出
File store type: NTFS
方法 2:使用用户自定义方法
在此方法中,将分配文件路径。然后,通过传递给定值来调用用户自定义方法,并根据算法,我们将了解 Java 中的文件存储类型。
示例
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main2
{
//main method
public static void main(String[] args)
{
//calling user defined method
checkFileStoreType();
}
//user defined method
static void checkFileStoreType()
{
//getting the file path
Path path = Paths.get("C:/Users/SAMPLE/Desktop/Learn/Program/myfile.txt");
//try block
try {
//getting the FileStore object
FileStore store = Files.getFileStore(path);
//getting the file store type
String type = store.type();
//print the result
System.out.println("File store type: " + type);
//catch block
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出
File store type: NTFS
在本文中,我们探讨了如何使用 Java 编程语言来了解文件存储类型。
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP