Android 读取文件大小

import java.io.File;
 
public class Main {
    
    
    public static void main(String[] args) {
    
    
        String filePath = "/sdcard/wangrui.txt"; // 要读取的文件路径
        
        File file = new File(filePath);
        long sizeInBytes = file.length();
        double sizeInKB = (double)sizeInBytes / 1024;
        double sizeInMB = (double)sizeInBytes / (1024 * 1024);
        
        System.out.println("文件大小为 " + sizeInBytes + " 字节");
        System.out.println("文件大小为 " + sizeInKB + " KB");
        System.out.println("文件大小为 " + sizeInMB + " MB");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_27494201/article/details/135355462