6、java如何计算文件的大小

package com.tij.io.file;

import java.io.File;

public class FileSize {

	/**
	 * This class Shows how to get file size in Java
	 * @param args
	 */
	public static void main(String[] args) {
		File file = new File("/Users/GYJ/Documents/bad_kpi.sql");
		if (file.exists()) {
			System.out.println(getFileSizeMegaBytes(file));
			System.out.println(getFileSizeKiloBytes(file));
			System.out.println(getFileSizeBytes(file));
		} else {
			System.out.println("File doesn't exists");
		}
	}
	
	private static String getFileSizeMegaBytes(File file) {
        return (double) file.length()/(1024*1024)+" mb";
    }
 
    private static String getFileSizeKiloBytes(File file) {
        return (double) file.length()/1024+"  kb";
    }
 
    private static String getFileSizeBytes(File file) {
        return file.length()+" bytes";
    }

}
out put===========
55.85526180267334 mb
57195.7880859375  kb
58568487 bytes

你可能感兴趣的:(java)