使用getResourcesAsStream()
项目中有一个配置文件user.properties,用来配置一些用户账号等信息。放在src/main/resources下,在项目中跑的好好的,打包后就读不到了。我原来是使用
Xxx.class.getClassLoader().getResource("user.properties")
打包后发现找不到文件,打印之,发现路径中多了一个!号。
xxx.jar!/user.properties
参考别人的博客后,稍微懂了点。改而使用 getResourcesAsStream()的方式获得输入流得到数据,至于作者说
通过Class类的getResourceAsStream()方法来获取 ,这种方法是如何读取jar中的资源文件的,这一点对于我们来说是透明的。
看看原理
想看看怎么实现的,所以看了一下源码,发现getResourceAsStream内部还是调用getResource。只不过它不是使用url.getPath()方法获得路径而后使用File来定位文件。而是使用openStream()方法,不解,为什么这样? 通过url.openStream()就可以获取了?打断点,追进去。
getResourceAsStream()方法如下,其中调用了url.openStream()
public InputStream getResourceAsStream(String name) {
URL url = getResource(name);
try {
return url != null ? url.openStream() : null;
} catch (IOException e) {
return null;
}
}
openStream()方法如下,调用了openConnection()
public final InputStream openStream() throws java.io.IOException {
return openConnection().getInputStream();
}
openConnection中涉及到了一个handler
public URLConnection openConnection() throws java.io.IOException {
return handler.openConnection(this);
}
看看handler是什么,看到有一群Handler,注意到有两个file和jar。
当我使用如下代码获得user.properties时,使用的是package sun.net.www.protocol.file
中的Handler。那么可以设想,如果在Jar包中,应该用的是sun.net.www.protocol.jar
中的Handler去处理。
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("user.properties");
但是因为没办法在jar包中打断点,所以我就直接把打包后的jar包拉过来,直接在代码中读取试试看,如下把项目打成jar包后,放到resources目录下,然后使用JarURLConnection去读取文件user.properties中的内容
public static void main(String[] args) throws IOException, InterruptedException {
URL fileUrl = new URL( "jar:file:///E:/tmp/selfmanager/selfmanager-console/src/main/resources/selfmanager-console-1.0-SNAPSHOT.jar!/user.properties");
JarURLConnection jarURLConnection = new JarURLConnection(fileUrl, null);
InputStream inputStream = jarURLConnection.getInputStream();
byte[] bytes = new byte[inputStream.available()];
is.read(bytes);
System.out.println(new String(bytes));
}
}
结果是可行的,user.properties中的内容被成功读取出来了。所以我想使用getResourceAsStream()中,当对应jar包路径时,应该也是选择这个Handler来读取文件的。不过要注意的是在编译过程中提示该类是内部专用。。。所以还是用getResourceAsStream()吧。
sun.net.www.protocol.jar.JarURLConnection是内部专用 API, 可能会在未来发行版中删除
搞了大半天,需求变了,因为user.properties中的内容如果打入jar包中,每次更新user.properties中的内容,都需要上传一次jar包,麻烦,嫌弃~后来实现的方式:把user.properties放到和jar包同级的目录,实时监控user.properties文件,如果有改变则读取内容。
得到当前jar包所在目录
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath(); path = path.substring(1,path.length()); int endIndex = path.lastIndexOf("/"); path = path.substring(0, endIndex); try { path = java.net.URLDecoder.decode(path, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(path);
然后结合 WatchService 实时监控该目录下的user.properties文件,达到实时更改配置能够刷新的效果。
WatchService watchService = FileSystems.getDefault().newWatchService(); Paths.get(path).register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); while (true) { WatchKey key = watchService.take(); for (WatchEvent<?> event: key.pollEvents()) { System.out.println("事件发生:" + event.context() + " " + event.kind()); } boolean valid = key.reset(); if (!valid) { break; } }