以下实例演示了使用 File 类的 file.exists() 和 file.length() 方法来获取文件大小,以字节计算(1KB=1024字节 ):
- /*
- author by shouce.ren
- Main.java
- */
- import java.io.File;
- public class Main {
- public static long getFileSize(String filename) {
- File file = new File(filename);
- if (!file.exists() || !file.isFile()) {
- System.out.println("文件不存在");
- return -1;
- }
- return file.length();
- }
- public static void main(String[] args) {
- long size = getFileSize("c:/java.txt");
- System.out.println("java.txt文件大小为: " + size);
- }
- }
以上代码运行输出结果为(java.txt 文件位于 C 盘):
- java.txt文件大小为: 480