文件类长length() (File Class long length())
This method is available in package java.io.File.length().
软件包java.io.File.length()中提供了此方法。
This method is used to return the length of the file in bits which is represented by the filepath.
此方法用于返回文件长度(以文件路径表示)。
This method returns the length of the file and in case if the file does not exist or empty file or exception occurs then it will return 0L.
此方法返回文件的长度,如果文件不存在或为空文件或发生异常,则它将返回0L 。
Syntax:
句法:
long length(){
}
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, which is the length of the file in bits.
此方法的返回类型为long ,这是文件的长度(以位为单位)。
Java程序演示length()方法的示例 (Java program to demonstrate example of length() 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 FileLength {
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:\\Users\\computer clinic\\OneDrive\\Articles\\java.txt");
// By using length() method return the length of the file 1 in bits
// because here file exists.
System.out.println("The Length Of The File1 is " + " " + file1.length());
// By using length() method return 0L because here file does not exists.
System.out.println("The Length Of The File2 is " + " " + file2.length());
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
输出量
D:\Programs>javac FileLength.java
D:\Programs>java FileLength
The Length Of The File1 is 45
The Length Of The File2 is 0
翻译自: https://www.includehelp.com/java/file-class-long-length-method-with-example.aspx