可以使用java.io.File.length()方法显示由抽象路径名指定的当前文件的长度。该方法以字节为单位返回文件长度,并且不需要任何参数。
演示此的程序如下所示-
示例import java.io.File;
public class Demo {
public static void main(String[] args) {
try {
File file = new File("demo1.txt");
file.createNewFile();
System.out.println("Length of file: " + file.length());
} catch(Exception e) {
e.printStackTrace();
}
}
}
上面程序的输出如下-
输出结果Length of file: 0
现在让我们了解上面的程序。
方法java.io.File.length()用于查找由抽象路径名指定的当前文件的长度并显示它。证明这一点的代码片段如下-try {
File file = new File("demo1.txt");
file.createNewFile();
System.out.println("Length of file: " + file.length());
} catch(Exception e) {
e.printStackTrace();
}