小米盒子2:java中测试文件大小问题?

来源:百度文库 编辑:高考问答 时间:2024/05/06 06:40:00
import java.io.*;
class file{
public static void main(String[] args)throws IOException{
File f=new File("c:\\music.mp3");
if(f.exists()){
System.out.println("文件存在");
System.out.println("文件大小为:"+f.length()/1024/1024+"M");

}
else{
f.createNewFile();
System.out.println("文件不存在");
}
}
}

music文件是4.87M
可是我执行上面的程序就显示4M,我想显示4.87M怎么办呢

强制转换为double类型,(double)(f.length()/1024/1024)