文件操作案例 获取某个文件夹下的所有指定文件
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class DigFileAll {
/**
* 返回指定文件夹下的所有的文件和文件夹的集合
*
* @param 指定文件夹
* @return 文件夹与文件的集合
*/
public static List digFile(File f, String end) {
File[] files = f.listFiles((dir, name) -> {
if (end.equals("null")) {
return true;
}
if (name.endsWith("." + end) || dir.isDirectory()) {
return true;
}
return false;
});
List fs = new ArrayList();
// 判断指定文件夹是否是文件,若是则返回一个空集合
if (f.isFile()) {
return fs;
}
// 判断指定文件夹是否为空文件夹
if (files.length != 0) {
// 将该文件夹下一层的文件和文件夹加入集合
for (File file : files) {
if (end.equals("null")) {
fs.add(file);
} else if (file.getName().endsWith("." + end)) {
fs.add(file);
}
}
// 获取下一层文件的所有文件夹加入集合
for (File file : files) {
if (digFile(file, end) != null) {
fs.addAll(digFile(file, end));
}
}
}
return fs;
}
/**
* 程序入口
*
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 输入文件名
System.out.println("请输入文件夹的名称");
String path = sc.next();
System.out.println("请输入要获取的文件类型");
String end = sc.next();
System.out.println("请决定文件以什么方式排序输出");
System.out.println("1.修改日期\n2.文件大小\n3.文件名排序");
int sortType = sc.nextInt();
File file = new File(path);
sc.close();
// 判断文件是否存在
if (!file.exists()) {
System.out.println("输入错误");
return;
}
// 获取文件夹下所有的文件和文件夹
List fs = digFile(file, end);
if (fs.isEmpty()) {
System.out.println("这是个空文件夹或是文件");
}
//将所有文件以文件名排序
Collections.sort(fs, (f1, f2) -> {
if (f1.getName().compareTo(f2.getName()) > 0) {
return 1;
} else if (f1.getName().compareTo(f2.getName()) == 0) {
return 0;
}
return -1;
});
if (sortType == 1) {
// 将所有文件夹和文件以最后修改时间进行排序
Collections.sort(fs, (f1, f2) -> {
if (f1.lastModified() > f2.lastModified()) {
return 1;
} else {
return -1;
}
});
} else if (sortType == 2) {
// 将所有文件夹和文件以文件大小进行排序
Collections.sort(fs, (f1, f2) -> {
if (f1.length() > f2.length()) {
return 1;
} else {
return -1;
}
});
} else {
// 不排序
}
// 将文件夹靠前,文件靠后排序
Collections.sort(fs, (f1, f2) -> {
if (f1.isDirectory() && f2.isFile()) {
return -1;
} else if (f2.isDirectory() && f1.isFile()) {
return 1;
}
return 0;
});
// 将排序后的文件夹输出
System.out.println("文件名\t\t文件大小\t\t文件修改时间");
fs.forEach(i -> {
// 确定文件修改时间的输出格式
Date date = new Date(i.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
// 确定文件大小输出格式
double size = i.length();
// 文件大小单位
String[] unit = { "B", "KB", "MB", "GB", "TB" };
int j = 0;
while (size > 1024) {
size /= 1024;
size = (long) (size * 1000) / 1000.0;
j++;
}
String finalSize;
if ((size + unit[j]).length() < 8) {
finalSize = size + unit[j] + "\t\t";
} else {
finalSize = size + unit[j] + "\t";
}
// 确定文件名输出格式
String name = i.getName();
// 获取文件名中中文字符与其他字符的个数
int chor = 0;
int chnr = 0;
char[] names = name.toCharArray();
for (char ch : names) {
if (ch > 127 || ch < 0) {
chnr++;
} else {
chor++;
}
}
// 文件名过长就截取并省略,长度不足就用\t补充
if (i.getName().length() >= 15) {
name = i.getName().substring(0, 14) + "…\t";
} else if (i.getName().length() >= 8 || chor + chnr * 2 > 8) {
name = i.getName() + "\t";
} else {
name = i.getName() + "\t\t";
}
if (chor + chnr * 2 >= 15) {
name = i.getName().substring(0, (14 - chnr)) + "…\t";
}
// 输出文件信息
System.out.println(name + finalSize + time);
});
}
}