一:统计一个文件夹中每种文件的个数并打印
如:
txt:3个
doc:1个
png:3个
二:将某个文件夹下的一级文件对象,按照最近修改时间降序展示,并显示修改时间(解析过)
如:
2.docx: 2022-07-04 16:30:52
1.txt: 2022-07-04 15:56:26
1.png: 2022-04-19 10:26:10
public class Test5 {
public static int countTxt;
public static int countDoc;
public static int countPng;
public static void main(String[] args) {
File f = new File("D:/picture");
String[] list = f.list();
for (String s : list) {
if (s.contains("txt")){
countTxt++;
}else if(s.contains("doc")){
countDoc++;
}else if (s.contains("png")){
countPng++;
}
}
System.out.println("txt:" + countTxt + "个");
System.out.println("doc:" + countDoc + "个");
System.out.println("png:" + countPng + "个");
File f2 = new File("D:/picture/aaa/bbb");
File[] list1 = f2.listFiles();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<String, Long> map = new HashMap<>();
for (File file : list1) {
map.put(file.getName(), file.lastModified());
}
List<Map.Entry<String, Long>> tem = new ArrayList(map.entrySet());
Collections.sort(tem, (o1, o2) -> o2.getValue().compareTo(o1.getValue()));
for (Map.Entry<String, Long> string : tem) {
System.out.println(string.getKey() + ": " + sdf.format(string.getValue()));
}
}
}