所寫工具類背景:項目經理叫我寫個工具類實現:給個項目的根目錄分析java文件及jsp文件。記錄文件類型、路徑、文件名和包名。java
定義的實體類(這裏我用了easypoi之後方便寫入excel文檔)jsp
@Data public class ReadExcelDataEntity implements Serializable { @Excel(name = "文件", orderNum = "1") private String fileType; @Excel(name = "名稱", orderNum = "2") private String fileName; @Excel(name = "路徑", orderNum = "3") private String filePath; @Excel(name = "包名", orderNum = "4") private String packageName; }
工具類:工具
public class FindAllFile { private static List<String> allList = new ArrayList<>(); /** * 根據根目錄獲取全部路徑信息 * * @param f File * @return allList */ public static List<String> getFile(File f) { File[] fList = f.listFiles(); for (int i = 0; i < fList.length; i++) { if (fList[i].isDirectory() == true) { getFile(fList[i]); } else { allList.add(fList[i].getAbsolutePath()); } } return allList; } /** * 根據類型獲取class一覽 * * @param f File * @param fileType 文件類型 * @return */ public static List<ReadExcelDataEntity> findAllFileByFileType(File f, String fileType) { //清空靜態allList屢次調用影響數據 allList.clear(); List<ReadExcelDataEntity> list = new ArrayList<>(); String rootPath = f.getPath(); String root = rootPath.replace("\\", "\\\\"); getFile(f); allList.stream() .forEach(x -> { if (x.endsWith(fileType)) { ReadExcelDataEntity entity = new ReadExcelDataEntity(); //獲取文件類型 String[] split = x.split("\\."); String type = split[split.length - 1]; File file = new File(x); //獲取文件名稱 String name = file.getName(); //獲取文件路徑 String filePath = x.replaceAll(root, " "); entity.setFileType(type); entity.setFilePath(filePath.trim()); entity.setFileName(name); FileReader fr = null; BufferedReader br = null; if ("java".equals(fileType)) { //獲取文件類型爲java的包名 try { fr = new FileReader(file); br = new BufferedReader(fr); String line = ""; while ((line = br.readLine()) != null) { if (line.indexOf("package") != -1) { line = line.replaceAll("package", " "); entity.setPackageName(line.trim()); list.add(entity); return; } } } catch (Exception e) { e.printStackTrace(); } finally { try { fr.close(); br.close(); } catch (Exception e) { e.printStackTrace(); } } } list.add(entity); } }); return list; } /** * 去除先後字符 * * @param args 傳入的字符串 * @param beTrim 去除格式 * @return */ public static String trim(String args, char beTrim) { int st = 0; int len = args.length(); char[] val = args.toCharArray(); char sbeTrim = beTrim; while ((st < len) && (val[st] <= sbeTrim)) { st++; } while ((st < len) && (val[len - 1] <= sbeTrim)) { len--; } return ((st > 0) || (len < args.length())) ? args.substring(st, len) : args; } }
核心代碼:測試
public static List<String> getFile(File f) { File[] fList = f.listFiles(); for (int i = 0; i < fList.length; i++) { if (fList[i].isDirectory() == true) { getFile(fList[i]); } else { allList.add(fList[i].getAbsolutePath()); } } return allList; }
測試類:spa
public class Test3 { public static void main(String[] args) { File file = new File("E:\\日本\\branches\\20190104\\skyreservesatellite"); List<ReadExcelDataEntity> java = FindAllFile.findAllFileByFileType(file, "jsp"); System.out.println(java); System.out.println(java.size()); } }
測試結果:excel
jsp文件:code
[ReadExcelDataEntity(fileType=jsp, fileName=addflightinfo.jsp, filePath=\skyreservesatellite\satellite\addflightinfo.jsp, packageName=null)]
java文件:blog
[ReadExcelDataEntity(fileType=java, fileName=UtilCheckerSso.java, filePath=\skyreservesatellite\satellite\src\sky\agency\checker\UtilCheckerSso.java, packageName=sky.agency.checker;)]