java.nio.file.Files的getFileStore()方法可帮助我们返回FileStore对象,该对象表示文件所在的文件存储。一旦获得对FileStore的引用,就可以应用fileStrore类型的操作来获取FileStore的信息。
用法:
public static FileStore
getFileStore(Path path)
throws IOException
参数:此方法接受参数路径,该路径是要获取FileStore的文件的路径。
返回值:此方法返回文件存储所在的文件存储。
异常:此方法将引发以下异常:
IOException:如果发生I /O错误
SecurityException:如果使用默认提供程序,并且已安装安全管理器,则调用SecurityManager.checkgetFileStore(String)方法以检查getFileStore对文件的访问
以下示例程序旨在说明getFileStore(Path)方法:
示例1:
// Java program to demonstrate
// Files.getFileStore() method
import java.io.IOException;
import java.nio.file.*;
public class GFG {
public static void main(String[] args)
{
// create object of Path
Path path
= Paths.get(
"D:\\Work\\Test\\file1.txt");
// get FileStore object
try {
FileStore fs
= Files.getFileStore(path);
// print FileStore name and block size
System.out.println("FileStore Name: "
+ fs.name());
System.out.println("FileStore BlockSize: "
+ fs.getBlockSize());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出:
示例2:
// Java program to demonstrate
// Files.getFileStore() method
import java.io.IOException;
import java.nio.file.*;
public class GFG {
public static void main(String[] args)
{
// create object of Path
Path path = Paths.get("C:\\data\\db");
// get FileStore object
try {
FileStore fs
= Files.getFileStore(path);
// print FileStore details
System.out.println("FileStore:"
+ fs.toString());
System.out.println("FileStore Free Space: "
+ fs.getUnallocatedSpace()
+ " Bytes");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出: