Cache java.net.URL when reading from files

It seems that java holds some cache in the URL (& files). for example I have a file "resourcs.txt" in the jar file in my class path. The contents of this file are: "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 modify the contents of the file as "Version 2" and call this code again. And I still get "Version 1"

How can I clear this cache.

Note. I found out that this only happens on Linux.

+5
source share
5 answers

- jar, URL-, sun.net.www.protocol.jar.JarURLConnection, , sun.net.www.protocol.jar.JarFileFactory

, setUseCache(false) on URLConnection .

Linux/Windows: URLJarFileCloseController Windows, , ...

+6

URL,

  URLConnection con = new URLConnection(new URL("jar", "", "file:test.jar!/resourcs.txt"));
  con.setUseCaches(false);
  new BufferedReader (new InputStreamReader(con.getInputStream())).readLine();
+3

, , sbridges, URLConnection "new URLConnection (...)", .

:

    URL url = new URL(urlSrt);
    URLConnection con = url.openConnection();
    con.setUseCaches(false);
+3
source

I think this is some kind of class loading problem because it is jar-protocoll.

Try to open the jar as a zip file.

ZipFile zf = new ZipFile(file);
try {
  InputStream in = zf.getInputStream("resourcs.txt");
  // ... read from 'in' as normal
} finally {
  zf.close();
}
+1
source

If you use some kind of third-party code, this should work:

url.openConnection().setDefaultUseCaches(false);
0
source

All Articles