public classFindAllFile {private static List allList = new ArrayList<>();/*** 根据根目录获取所有路径信息
*
*@paramf File
*@returnallList*/
public static ListgetFile(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());
}
}returnallList;
}/*** 根据类型获取class一览
*
*@paramf File
*@paramfileType 文件类型
*@return
*/
public static ListfindAllFileByFileType(File f, String fileType) {//清空静态allList多次调用影响数据
allList.clear();
List list = new ArrayList<>();
String rootPath=f.getPath();
String root= rootPath.replace("\\", "\\\\");
getFile(f);
allList.stream()
.forEach(x->{if(x.endsWith(fileType)) {
ReadExcelDataEntity entity= newReadExcelDataEntity();//获取文件类型
String[] split = x.split("\\.");
String type= split[split.length - 1];
File file= newFile(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= newFileReader(file);
br= newBufferedReader(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);
}
});returnlist;
}/*** 去除前后字符
*
*@paramargs 传入的字符串
*@parambeTrim 去除格式
*@return
*/
public static String trim(String args, charbeTrim) {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;
}
}