import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class FileViewer {
public static List<String> getListFiles(String path, String suffix,
boolean isdepth) {
List<String> lstFileNames = new ArrayList<String>();
File file = new File(path);
return FileViewer.listFile(lstFileNames, file, suffix, isdepth);
}
private static List<String> listFile(List<String> lstFileNames, File f,
String suffix, boolean isdepth) {
// 若是目录, 采用递归的方法遍历子目录
if (f.isDirectory()) {
File[] t = f.listFiles();
for (int i = 0; i < t.length; i++) {
if (isdepth || t[i].isFile()) {
listFile(lstFileNames, t[i], suffix, isdepth);
}
}
} else {
String filePath = f.getAbsolutePath();
if (!suffix.equals("")) {
int begIndex = filePath.lastIndexOf("."); // 最后一个.(即后缀名前面的.)的索引
String tempsuffix = "";
if (begIndex != -1) {
tempsuffix = filePath.substring(begIndex + 1,
filePath.length());
if (tempsuffix.equals(suffix)) {
lstFileNames.add(filePath);
}
}
} else {
lstFileNames.add(filePath);
}
}
return lstFileNames;
}
// 递归取得文件夹(包括子目录)中所有文件的大小
public static long getFileSize(File f) throws Exception// 取得文件夹大小
{
long size = 0;
File flist[] = f.listFiles();
for (int i = 0; i < flist.length; i++) {
if (flist[i].isDirectory()) {
size = size + getFileSize(flist[i]);
} else {
size = size + flist[i].length();
}
}
return size;
}
public static String FormetFileSize(long fileS) {// 转换文件大小
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
public static void main(String[] args) {
File file = new File("D:\\tools");
List<String> list = getListFiles("D:\\tools", "", true);
for (String fileName : list) {
System.out.println(fileName);
}
System.out.println(list.size());
try {
System.out.println(FormetFileSize(getFileSize(file)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
java 获取文件夹所有文件,大小,文件名
最新推荐文章于 2024-06-19 12:55:05 发布