package filetest;import java.io.File;import java.io.IOException;public class FileEdit {double size=0.0;//计算文件或文件夹的大小,单位MBpublic double getSize(File file){//判断文件是否存在if(file.exists()) {if(!file.isFile()) {//获取文件大小File[] fl = file.listFiles();double ss=0;for(File f : fl)ss += getSize(f);return ss;}else {double ss = (double) file.length()/1024/1024;System.out.println(file.getName()+":"+ss+"MB");return ss;}}else {System.out.println("文件或文件夹不存在,请检查文件路径是否正确!");return 0.0;}}public static void main(String[] args) throws IOException{FileEdit fd = new FileEdit();double all = fd.getSize(new File("C:\\Users\\FuHeishi826\\Desktop\\壹青年"));System.out.println("All: "+all+"MB");}}