java file 根目录_java根据所给的根目录获取底下所有文件夹结构

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值