用 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();
}
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP