从文件读取时,java.net.URL缓存 [英] java.net.URL cache when reading from files
问题描述
例如
我在我的类路径的jar文件中有一个resourcs.txt文件。
这个文件的内容是:
版本1
pre $ new java.io.BufferedReader new java.io.InputStreamReader(new URL(jar,,file:test.jar!/resourcs.txt)。openConnection()。getInputStream()))。readLine()
<
$ b
返回版本1(如预期)
文件内容为版本2,再次调用此代码。
我仍然得到版本1
我怎样才能清除这个缓存?
注意:我发现它只发生在Linux上。
因为 jar
在你的URL中使用的协议,连接是sun.net.www.protocol.jar.JarURLConnection ,它可以从 sun.net.www.protocol.jar.JarFileFactory 源代码在 URLConnection
setUseCache(false) >
我对Linux / Windows行为差异的假设:来自URLJarFileCloseController接口的关闭事件通知是t在Windows上操纵速度更快,因为它不会使文件句柄保持打开状态的时间太长...
It seems like java is holding some kind of a cache to URL (& files). e.g. I have a file "resourcs.txt" in a jar file in my classpath. The content of this file is: "Version 1"
new java.io.BufferedReader (new java.io.InputStreamReader( new URL("jar", "", "file:test.jar!/resourcs.txt").openConnection().getInputStream())).readLine()
returns "Version 1" (as expected)
I change the file content to be "Version 2" and call again to this code. And I still get "Version 1"
How can I clear this "cache".
Notice: I found out it only happens on Linux.
Because of the jar
protocol used in your URL, the connection is an instance of sun.net.www.protocol.jar.JarURLConnection which takes benefit from a cache implemented in sun.net.www.protocol.jar.JarFileFactory
Source code confirms a setUseCache(false)
on URLConnection
implementation will prevent the use of that cache.
My hypothesis about the Linux/Windows behavior difference: the close event notification from URLJarFileCloseController interface is triggered faster on Windows because it does not appreciate to keep file handles opened for a too long period...
这篇关于从文件读取时,java.net.URL缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!