java读取classpath/package/jar下面任意文件
1.读取classpath下面任意文件(可以读取properties文件,也可以是任意其他类型的文件),废话 不多说,直接上代码:
-
// 读取classpath下面任意文件
-
URL url = ClassLoader.getSystemResource("one.properties");
-
// url.getFile() 得到这个文件的绝对路径 /D:/work/workspace/spring-datasource/target/classes/one.properties
-
InputStream inputStream = new FileInputStream(url.getPath());
-
Properties properties = new Properties();
-
properties.load(inputStream);
-
-
for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
-
System.out.println(objectObjectEntry.getKey() ":" objectObjectEntry.getValue());
-
}
-
-
System.out.println(ClassLoader.getSystemResourceAsStream("one.properties"));
-
-
ClassLoader classLoader = MyTest.class.getClassLoader();
-
System.out.println(classLoader.getResourceAsStream("one.properties"));
-
System.out.println(classLoader.getResource("one.properties"));
-
System.out.println(MyTest.class.getResource("/one.properties"));
-
System.out.println(MyTest.class.getResourceAsStream("/one.properties"));
执行效果如下:
2.读取一个package路径下面所有的class,代码如下:
-
List<Class> fromPackage = getClasssFromPackage("com.laozeng.test");
-
for (Class aClass : fromPackage) {
-
System.out.println(aClass.getName());
-
}
-
-
-
/**
-
* 获得包下面的所有的class
-
* @param pack package完整名称
-
* @return List包含所有class的实例
-
*/
-
public static List<Class> getClasssFromPackage(String pack) {
-
List<Class> clazzs = new ArrayList<>();
-
-
// 是否循环搜索子包
-
boolean recursive = true;
-
-
// 包名字
-
String packageName = pack;
-
// 包名对应的路径名称
-
String packageDirName = packageName.replace('.', '/');
-
-
Enumeration<URL> dirs;
-
-
try {
-
dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
-
while (dirs.hasMoreElements()) {
-
URL url = dirs.nextElement();
-
-
String protocol = url.getProtocol();
-
-
if ("file".equals(protocol)) {
-
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
-
findClassInPackageByFile(packageName, filePath, recursive, clazzs);
-
} else if ("jar".equals(protocol)) {
-
System.out.println("jar类型的扫描");
-
}
-
}
-
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
-
return clazzs;
-
}
-
-
/**
-
* 在package对应的路径下找到所有的class
-
*
-
* @param packageName
-
* package名称
-
* @param filePath
-
* package对应的路径
-
* @param recursive
-
* 是否查找子package
-
* @param clazzs
-
* 找到class以后存放的集合
-
*/
-
public static void findClassInPackageByFile(String packageName, String filePath, final boolean recursive, List<Class> clazzs) {
-
File dir = new File(filePath);
-
if (!dir.exists() || !dir.isDirectory()) {
-
return;
-
}
-
// 在给定的目录下找到所有的文件,并且进行条件过滤
-
File[] dirFiles = dir.listFiles(file -> {
-
boolean acceptDir = recursive && file.isDirectory();// 接受dir目录
-
boolean acceptClass = file.getName().endsWith("class");// 接受class文件
-
return acceptDir || acceptClass;
-
});
-
-
for (File file : dirFiles) {
-
if (file.isDirectory()) {
-
findClassInPackageByFile(packageName "." file.getName(), file.getAbsolutePath(), recursive, clazzs);
-
} else {
-
String className = file.getName().substring(0, file.getName().length() - 6);
-
try {
-
clazzs.add(Thread.currentThread().getContextClassLoader().loadClass(packageName "." className));
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
效果如下:
3.获取jar文件包中的package下面的所有class,代码如下:
-
List<Class> fromJarFile = getClasssFromJarFile("D:\\work\\myrep\\com\\alibaba\\druid\\1.2.14\\druid-1.2.14.jar", "com/alibaba/druid/wall");
-
fromJarFile = getClasssFromJarFile("D:\\work\\myrep\\com\\alibaba\\druid\\1.2.14\\druid-1.2.14.jar", null);
-
for (Class aClass : fromJarFile) {
-
System.out.println(aClass.getName());
-
}
-
-
/**
-
* 从jar文件中读取指定目录下面的所有的class文件
-
*
-
* @param jarPaht jar文件存放的位置
-
* @param filePaht 指定的文件目录
-
* @return 所有的的class的对象
-
*/
-
public static List<Class> getClasssFromJarFile(String jarPaht, String filePaht) {
-
List<Class> clazzs = new ArrayList<>();
-
-
JarFile jarFile = null;
-
try {
-
jarFile = new JarFile(jarPaht);
-
} catch (IOException e1) {
-
e1.printStackTrace();
-
}
-
-
List<JarEntry> jarEntryList = new ArrayList<>();
-
-
Enumeration<JarEntry> ee = jarFile.entries();
-
while (ee.hasMoreElements()) {
-
JarEntry entry = ee.nextElement();
-
if (Objects.equals(filePaht, null)) {
-
if (entry.getName().endsWith(".class")) {
-
jarEntryList.add(entry);
-
}
-
} else {
-
// 过滤我们出满足我们需求的东西
-
if (entry.getName().startsWith(filePaht) && entry.getName().endsWith(".class")) {
-
jarEntryList.add(entry);
-
}
-
}
-
}
-
for (JarEntry entry : jarEntryList) {
-
String className = entry.getName().replace('/', '.');
-
className = className.substring(0, className.length() - 6);
-
// 也可以采用如下方式把类加载成一个输入流
-
// InputStream in = jarFile.getInputStream(entry);
-
try {
-
// com.alibaba.druid.support.calcite.TDDLSqlSelect 加载类的时候,也会随之加载类中的其他类,这个时候会报错:Caused by: java.lang.ClassNotFoundException: org.apache.calcite.sql.SqlSelect
-
System.out.println(className);
-
clazzs.add(Thread.currentThread().getContextClassLoader()
-
.loadClass(className));
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
return clazzs;
-
}
效果如下:
4.读取当前工程下面所有jar和classpath下面指定的配置文件(这也是spring加载配置文件的原理),代码如下:
-
Properties properties = load("META-INF/spring.handlers");
-
for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
-
System.out.println(objectObjectEntry.getKey() ":" objectObjectEntry.getValue());
-
}
-
-
-
private static Properties load(String resourceName) throws Exception{
-
Enumeration<URL> urls = ClassLoader.getSystemResources(resourceName);
-
Properties props = new Properties();
-
while (urls.hasMoreElements()) {
-
URL url = urls.nextElement();
-
URLConnection con = url.openConnection();
-
ResourceUtils.useCachesIfNecessary(con);
-
InputStream is = con.getInputStream();
-
props.load(is);
-
is.close();
-
}
-
return props;
-
}
效果如下,可以看到本工程里面的也加载出来了:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhaccc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24