用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 兄妹拍照祝福语简短的话