Java的.nio.file.FileStore类在Java中
Java的.nio.file是在Java中一个包,包括文件存储类。 FileStore 类是一个类,它提供了用于对文件存储执行某些操作的方法。
- FileStore 是一个从Java.lang 包扩展 Object 的类。 FileStore 类可以从Java.lang.Object 包继承的几个方法是 clone()、equals()、finalize()、getClass()、hashCode()、notify()、notifyAll()、toString()、wait( )。
- getFileStore() 是 FileStore 类提供的一个方法,它被调用以了解文件的存储位置,通常它会告诉文件存储在 CD 驱动器中的位置。
- 并且 FileStore 还支持少数或更多类,如 FileStoreAttributeView,它提供一组文件存储属性的只读或向上视图。
语法:类声明
public abstract class FileStore extends Object
{
abstract CheckResult( );
// Here an abstract method which
// don't have usually a method definition part.
}
注意:每当你为抽象类创建一个对象并试图在编译时调用它时,编译器会抛出一个错误,说“方法不完整”,这是因为抽象被称为不完整,这意味着你不能为这样的方法创建对象这个。
FileStore 类的构造函数如下:Constructor Description FileStore() This constructor Initializes a new instance of this class.
FileStore类的方法如下:
Methods | Description |
---|---|
getAttribute(String attribute) | This method reads the value of a file store attribute. |
getFileStoreAttributeView(Class | This method returns a FileStoreAttributeView of the given type. |
getTotalSpace() | This method returns the size, in bytes, of the file store. |
getUnallocatedSpace() | This method returns the number of unallocated bytes in the file store. |
getUsableSpace() | This method returns the number of bytes available to this Java virtual machine on the file store. |
isReadOnly() | This method tells whether this file store is read-only. |
name() | This method returns the name of this file store. |
supportsFileAttributeView(Class extends FileAttributeView> type) | This method tells whether this file store supports the file attributes identified by the given file attribute view. |
supportsFileAttributeView(String name) | This method tells whether this file store supports the file attributes identified by the given file attribute view. |
type() | This method returns the type of this file store. |
示例 1:
Java
// Java Program to demonstrate FileStore Class
// with its methods
// Importing required libraries
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
// Main class
public class GFG {
// Declaring and initializing variable
static long Bytes = 1000;
// Main driver method
public static void main(String[] args) throws Exception
{
// Creating an object of FileSystem class
FileSystem fileSystem = FileSystems.getDefault();
for (FileStore fileStore :
fileSystem.getFileStores()) {
// Here we use Bytes to
// get the usable space in terms of bytes.
// Here getUsableSpace method is used to
// know the free space in the drive.
// then it writtens back the value into
// usableSpace variable
long usableSpace
= fileStore.getUsableSpace() / Bytes;
// Here we use Bytes to
// get the used space in terms of bytes.
// Here we get the usedspace value by
// subtracting the methods given below.
long usedSpace = (fileStore.getTotalSpace()
- fileStore
.getUnallocatedSpace())
/ Bytes;
// Readonly writes true or false based on
// the mode the we file open.
boolean readOnly = fileStore.isReadOnly();
// Print and display the information
// that the methods are allocated with
System.out.println(
"All information on the FileStore");
// Print and display used and unused space
System.out.println("Used Space : " + usedSpace);
System.out.println("Un-Used Space : "
+ usableSpace);
// Print boolean true false whether it is read
// only
System.out.println("Is this read only : "
+ readOnly);
}
}
}
Java
// Java Program to demonstrate FileStore Class
// with its methods
// Importing required libraries
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
// Main class
// FileStoreExample
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Creating an object of FileSystem class
FileSystem fileSystem = FileSystems.getDefault();
// Iterating for file storage using for each loop
for (FileStore fileStore :
fileSystem.getFileStores()) {
// Here filestore() is used to know the
// folder/drive name where the actual file is
// getting stored
String fileStoreName = fileStore.name();
// This method returns the fileStore type
String fileStoreType = fileStore.type();
// Print and display commands
// 1. information of file
System.out.println(
"All information on the FileStores\n\n");
// 2. Name of a file stored
System.out.println("File Store Name : "
+ fileStoreName);
// 3. Type of file stored
System.out.println("File Store Type : "
+ fileStoreType);
}
}
}
输出:
示例 2:
Java
// Java Program to demonstrate FileStore Class
// with its methods
// Importing required libraries
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
// Main class
// FileStoreExample
public class GFG {
// Main driver method
public static void main(String[] args) throws Exception
{
// Creating an object of FileSystem class
FileSystem fileSystem = FileSystems.getDefault();
// Iterating for file storage using for each loop
for (FileStore fileStore :
fileSystem.getFileStores()) {
// Here filestore() is used to know the
// folder/drive name where the actual file is
// getting stored
String fileStoreName = fileStore.name();
// This method returns the fileStore type
String fileStoreType = fileStore.type();
// Print and display commands
// 1. information of file
System.out.println(
"All information on the FileStores\n\n");
// 2. Name of a file stored
System.out.println("File Store Name : "
+ fileStoreName);
// 3. Type of file stored
System.out.println("File Store Type : "
+ fileStoreType);
}
}
}
输出