Automatic translation

Archives

May 2008
The My Me J V S D
"April June "
A 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Contributors

ClassLoader and reload files

Have you ever needed to reload a file placed in one of the directories appear in the CLASSPATH environment variable? Usually, these files are loaded into the memory of the JVM using the method "getResourceAsStream" class "java.lang.ClassLoader". However, once the files are loaded by a ClassLoader, they remain in memory and are no longer read from disk or network. To force reloading, simply instantiate a new ClassLoader. This is done in a few lines of Java:


 public class ResourceLoader {
         public static InputStream getResourceAsStream (String name) {
           ClassLoader loader = new URLClassLoader (constructUrls ()) {
             public URL getResource (String name) {return FindResource (name);}
           };
           loader.getResourceAsStream return (name);
         }

         private static URL [] constructUrls () {
           String classpath = System.getProperty ("java.class.path");
           String [] paths = classpath.split (File.pathSeparator);
           int length = paths.length;
           URL [] = new URL arrayOfUrls [length];

           try {
             for (int i = 0; i <length; i + +)
               arrayOfUrls [i] = new File (paths [i]). Touri (). toURL ();
           }
           catch (MalformedURLException e) {...  }

           arrayOfUrls return;
         }
       }