示例:递归某个指定目录,并输出此目录下的文件后缀名及后缀名出现的次数
1 package com.zelin.lesson.test; 2 3 import jdk.jfr.events.FileReadEvent; 4 import org.junit.Before; 5 import org.junit.Test; 6 import sun.net.smtp.SmtpProtocolException; 7 8 import java.io.File; 9 import java.io.FileNotFoundException; 10 import java.io.FileReader; 11 import java.io.IOException; 12 import java.text.NumberFormat; 13 import java.util.*; 14 15 public class TestTi { 16 17 /** 18 * @Author: feng.wang By 2019/8/6 15:27 19 * @param: 20 * @return: 21 * @Company: www.zelininfo.com 22 * @Descript: 23 * 案例一: 24 * 读取e:\\aa.txt文件,aa.txt中保存内容如下 25 * 23.9,12.34,43.90,89.67 26 * 要求把aa.txt中的数字精确到小数点后一位倒序输出 27 */ 28 @Test 29 public void test01() throws IOException { 30 //0.定义一个集合用于存放数字 31 List<Double> list = new ArrayList<>(); 32 //1.读取指定的文件 33 //1.1)定义一个文件对象 34 File file = new File("e:/aa.txt"); 35 //1.2)定义输入流对象 36 FileReader reader = new FileReader(file); 37 //1.3)定义字符数组 38 char[] ch = new char[(int) file.length()]; 39 //1.4)开始读取并将数据放到字符数组中 40 reader.read(ch); 41 //2.将字符数组转换为字符串 42 String string = new String(ch); 43 //3.根据,号进行拆分 44 String[] split = string.split(","); 45 //4.将数组转换为double后放到集合list中 46 //4.1)定义格式化对象,用于对数字进行格式化处理 47 NumberFormat numberFormat = NumberFormat.getInstance(); 48 //4.2)设置小数点后最多保留一位有效数字 49 numberFormat.setMaximumFractionDigits(1); 50 for (String s : split) { 51 //4.3)对数字进行格式化处理 52 String format = numberFormat.format(Double.parseDouble(s)); 53 //4.4)将上面的格式化后的字符串转换为double放到list中 54 list.add(new Double(format)); 55 } 56 //5.将list集合进行倒序排序 57 Collections.sort(list,Collections.reverseOrder()); 58 //6.输出 59 System.out.println(list); 60 61 } 62 /** 63 * @Author: feng.wang By 2019/8/6 15:39 64 * @param: 65 * @return: 66 * @Company: www.zelininfo.com 67 * @Descript: 68 * 案例二:通过所学的IO知识,将某个文件夹下的所有文件后缀名在控制台输出。 69 */ 70 @Test 71 public void test02(){ 72 getFile(new File("D:\\apache-tomcat-7.0.52")); 73 } 74 private void getFile(File file){ 75 if(file.isDirectory()){ //是目录就递归 76 //打印目录名 77 System.out.println(file.getAbsolutePath()); 78 for (File f : file.listFiles()) { 79 getFile(f); 80 } 81 }else{ //是文件就打印后缀名 82 String name = file.getName(); 83 //找到最后一个.所在的位置 84 int lastIndex = name.lastIndexOf("."); 85 //从最后一个.截取到结束就是后缀名 86 if(lastIndex != -1){ 87 String suffix = name.substring(lastIndex); 88 System.out.println(suffix); 89 } 90 91 } 92 } 93 /** 94 * 案例三 95 * 递归某个指定目录,并输出此目录下的文件后缀名及后缀名出现的次数 96 * 分析: 97 * ① 因为要查看目录,而目录是唯一的,所以可以将目录作为 key放到外层集合中。 98 * ② 因为要查看此目录下所有的后缀名及其出现的次数,所以可以将后缀名作为key,将其出现的次数作为值放到子集合中。 99 */ 100 private Map<String,Map<String,Integer>> maps; //大key:代表目录 小key:代表文件后缀名 value:代表此后缀名出现的次数 101 @Before 102 public void init(){ 103 maps = new HashMap<>(); 104 } 105 @Test 106 public void test03()throws Exception{ 107 findDir(new File("D:\\apache-tomcat-7.0.52")); 108 System.out.println(maps); 109 } 110 private void findDir(File file) throws Exception{ 111 if(file.isDirectory()){ //1.如果是目录,就向大集合放内容,其中的key就是当前目录名,其中的小map需要我们构造出来 112 //1.1)定义一个小map集合,用于存放此目录下的文件后缀名及出现的次数 113 Map<String,Integer> smallMap = new HashMap<>(); 114 //1.2)将此小集合放到外层大集合中 115 maps.put(file.getAbsolutePath(),smallMap); 116 //1.3)递归调用 117 File[] files = file.listFiles(); 118 for (File f : files) { 119 findDir(f); 120 } 121 }else{ //2.如果是文件,就取得文件的后缀名,可以根据文件的后缀名得到此文件后缀的次数 122 //2.1)得到当前文件的父路径 123 String parent = file.getParent(); 124 //2.2)在大map中根据key得到值(小map) 125 Map<String, Integer> smallMap = maps.get(parent); 126 //2.3)如果有此map,就根据此文件的后缀名从小map中取得其出现的次数,如果是第一次,则次数为1 127 if(smallMap != null){ 128 int indexOf = file.getName().lastIndexOf("."); //得到最后一个. 129 if(indexOf != -1){ 130 String suffix = file.getName().substring(indexOf); //得到文件后缀名 131 //2.4)判断这个小map中是否有此后缀 132 if(smallMap.containsKey(suffix)){ //如果此有此后缀名,就将原来的后缀名出现次数加1 133 //2.4.1)得到原来后缀名出现的次数 134 Integer count = smallMap.get(suffix); 135 smallMap.put(suffix,count+1); 136 }else{ //如果没有就将次数设置为1 137 smallMap.put(suffix,1); 138 } 139 } 140 } 141 } 142 } 143 }