Java获取文件或文件夹的大小
package com.ycy2;
import java.io.File;
public class Test01 {
public static void main(String[] args) {
File file = new File("E:\\test\\a.txt");
long filesLength = Test01.getFilesLength(file);
System.out.println("文件长度为:" + filesLength);
}
public static long getFilesLength(File file) {
long fileLength = 0;
if (file != null) {
if (file.isFile()) {
fileLength = file.length();
return fileLength;
}
File[] fileArray = file.listFiles();
if (fileArray != null) {
for (File f : fileArray) {
fileLength += getFilesLength(f);
}
}
} else {
System.out.println("file为空!");
}
return fileLength;
}
}