public static void getFileSize(String path){
//传入文件路径
File file = new File(path);
//测试此文件是否存在
if(file.exists()){
//如果是文件夹
//这里只检测了文件夹中第一层 如果有需要 可以继续递归检测
if(file.isDirectory()){
int size = 0;
for(File zf : file.listFiles()){
if(zf.isDirectory()) continue;
size += zf.length();
}
System.out.println("文件夹 "+file.getName()+" Size: "+(size/1024f)+"kb");
}else{
System.out.println(file.getName()+" Size: "+(file.length()/1024f)+"kb");
}
//如果文件不存在
}else{
System.out.println("此文件不存在");
}
}
File f= new File("你的文件路径");
system.out.println(f.length());
以上就是通过length获取文件的字节数。
public static void getFileSize(String path){
//传入文件路径
File file = new File(path);
//测试此文件是否存在
if(file.exists()){
//如果是文件夹
//这里只检测了文件夹中第一层 如果有需要 可以继续递归检测
if(file.isDirectory()){
int size = 0;
for(File zf : file.listFiles()){
if(zf.isDirectory()) continue;
size += zf.length();
}
System.out.println("文件夹 "+file.getName()+" Size: "+(size/1024f)+"kb");
}else{
System.out.println(file.getName()+" Size: "+(file.length()/1024f)+"kb");
}
//如果文件不存在
}else{
System.out.println("此文件不存在");
}
}
FileInputStream fis=new FileInputStream(new File(filepath));
System.out.println(fis.available());
当然里面需要处理下异常
io里面的file类,就可以了