本文主要是介绍通过packageName递归查找包下的文件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
*** Created with IntelliJ IDEA.* 通过packagename 递归查找 包下的文件* @Description:* @author: bowang* @create: 2020-06-26 下午**/
public class SearchPath {public static void main(String[] args) {SearchPath searchPath = new SearchPath();searchPath.scanPackage("com/bo/wang");}private String replaceTo(String packageName) {return packageName.replaceAll("\\.","/");}private void scanPackage(String packageName) {URL url = this.getClass().getClassLoader().getResource(replaceTo(packageName));String file = url.getFile();System.out.println(file);File f = new File(file);String[] list = f.list();for (String path : list) {File filePath = new File(file +"/"+path);if (filePath.isDirectory()) {scanPackage(packageName + "." + path);} else {System.out.println(packageName + "." + path);}}}
}
这篇关于通过packageName递归查找包下的文件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!