20080610 Tuesday June 10, 2008

Accessing a Java resource as a file

Here is some quick and dirty code to get access to a resource on the class path as a File object:

URL url = this.getClass().getResource("/the-file.txt");
URI uri = new URI(url.toExternalForm());
File f = new File(uri);

Obviously replace "/the-file.txt" with the actual path to the resource. Note: this is the path on the "class path", not the file system.

Posted by rickg ( Jun 10 2008, 10:55:38 AM PDT ) Permalink Comments [0]