我正在java中玩文件类。我试图确定文件的大小,所以我使用了length()方法。对于我使用的给定代码,我的输出是0。
File test=new File("hello.pdf");
System.out.println(test.length());
首先,我是否正确使用了长度?(就像它决定文件大小还是我读错了javadocs ?)
第二,如果我在正确的上下文中使用了length,为什么它是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!");
}
目录中是否有hello.pdf文件?如果没有,那么您只是创建了一个空的,长度为0字节。