我正在研究 java 中的文件类。我试图确定文件的大小,所以我使用了 length() 方法。对于我使用的给定代码,我的输出是 0。
File test=new File("hello.pdf");
System.out.println(test.length());
首先,我是否正确使用了长度?(它决定了文件大小还是我读错了 javadoc?)
第二,如果我在正确的上下文中使用长度,为什么它是 0?谢谢。
最佳答案
试试这个。
File test=new File("hello.pdf");
if(test.exists()){
double bytes = test.length();
double kilobytes = (bytes / 1024);
double megabytes = (kilobytes / 1024);
System.out.println("bytes : " + bytes);
System.out.println("kilobytes : " + kilobytes);
System.out.println("megabytes : " + megabytes);
}else{
System.out.println("File does not exists!");
}
关于java - 使用文件类确定文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22447162/