java file类的父类
文件类long getFreeSpace() (File Class long getFreeSpace())
This method is available in package java.io.File.getFreeSpace().
软件包java.io.File.getFreeSpace()中提供了此方法。
This method is used to return the size of unallocated space(Space in terms of bytes) which is represented in the file pathname if the path exists and if the path does not exists then it return 0L(i.e. it return a long integer value that's why it is 0L).
此方法用于返回未分配空间的大小(以字节为单位的空间),如果路径存在且路径不存在,则以文件路径名表示该空间,则返回0L(即,它返回一个长整数值,这就是为什么)它是0L)。
The return type of this method is long so the size of unallocated bytes is of long type.
此方法的返回类型很长,因此未分配字节的大小是长类型。
This method gives a rough idea that how much space remains or in other words how much space is free and the thing needs to remember that there is no guarantee of an exact number of unallocated bytes in the given pathname.
这种方法给出了一个粗略的想法,即剩余多少空间,换句话说,还有多少可用空间,需要记住的是,不能保证给定路径名中有未分配字节的确切数目。
This method may raise an exception (i.e. Security Exception) if the function does not allow to be a file created.
如果该函数不允许创建文件,则此方法可能会引发异常(即Security Exception)。
Syntax:
句法:
long getFreeSpace(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the File.
我们不会在File方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is long i.e. it returns the size of unallocated bytes else it return 0L if path does not exists.
此方法的返回类型很长,即它返回未分配字节的大小,否则,如果路径不存在,则返回0L。
Java程序演示getFreeSpace()方法的示例 (Java program to demonstrate example of getFreeSpace() method)
// import the File class because we will use File class methods
import java.io.File;
// import the Exception class because it may raise an exception
/// when working with files
import java.lang.Exception;
public class GetFreeSpace {
public static void main(String[] args) {
try {
// Specify the path of file and we use double slashes to
// escape '\' character sequence for windows otherwise
// it will be considerable as url.
File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
File file2 = new File("C:\\java.txt");
// By using getFreeSpace() return the size of free or
// unallocated space of the partition in the filepath
// because here filepath exists .
System.out.println("The Free Space in File 1 :" + " " + file1.getFreeSpace());
// By using getFreeSpace() return 0L because here filepath does not exists
System.out.println("This Free Space in File 2 :" + " " + file2.getFreeSpace());
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
输出量
D:\Programs>javac GetFreeSpace.java
D:\Programs>java GetFreeSpace
The Free Space in File 1 : 266079727616
This Free Space in File 2 : 0
翻译自: https://www.includehelp.com/java/file-class-long-getfreespace-method-with-example.aspx
java file类的父类