package filetest;
import java.io.*;
/**
* 文件处理
* @author weidong
*
*/
public class FileEdit {
double size = 0.0;
/**
* 计算文件或者文件夹的大小 ,单位 MB
* @param file 要计算的文件或者文件夹 , 类型:java.io.File
* @return 大小,单位:MB
*/
public 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("f:\\电影"));
System.out.println("ALL: " + all + "MB");
}
}
获取本地文件Java java获取本地文件大小
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java 根据HttpEntity 中contenttype获取文件类型
java 根据HttpEntity 中contenttype获取文件类型
apache HTTP Apache -
java程序中的堆在什么位置
堆区 young 区 S0+S1 S0,S1大小是一样的,在同一时间内,只有一个启用
java程序中的堆在什么位置 垃圾回收器 java Java