用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 升职祝福语朋友简短精辟