用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 出阁宴祝福语姐姐简短