用Java显示当前文件的长度
可以使用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();
}热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短