URL urlToResource = this.getClass().getResource("/custom.csv");
InputStream isToResource = this.getClass().getResourceAsStream("/custom.csv");As long as the resource is in the root of the JAR and you make sure to use a leading slash you should be able to resolve the resource consistently across platforms.
public abstract class PropertiesUtil {
public static final boolean THROW_ON_LOAD_FAILURE = true;
private static final boolean LOAD_AS_RESOURCE_BUNDLE = false;
private static final String SUFFIX = ".properties";
/**
* Looks up a resource named 'name' in the classpath. The resource must map
* to a file with .properties extention. The name is assumed to be absolute
* and can use either "/" or "." for package segment separation with an
* optional leading "/" and optional ".properties" suffix. Thus, the
* following names refer to the same resource:
*
* some.pkg.Resource
* some.pkg.Resource.properties
* some/pkg/Resource
* some/pkg/Resource.properties
* /some/pkg/Resource
* /some/pkg/Resource.properties
*
*
* @param name classpath resource name [may not be null]
* @param loader classloader through which to load the resource [null
* is equivalent to the system class loader]
*
* @return resource converted to java.util.Properties [may be null if the
* resource was not found and THROW_ON_LOAD_FAILURE is false]
* @throws IllegalArgumentException if the resource was not found and
* THROW_ON_LOAD_FAILURE is true
*/
public static Properties loadProperties(String name, ClassLoader loader) {
if (name == null) {
throw new IllegalArgumentException("null input: name");
}
if (name.startsWith("/")) {
name = name.substring(1);
}
if (name.endsWith(SUFFIX)) {
name = name.substring(0, name.length() - SUFFIX.length());
}
Properties result = null;
InputStream in = null;
try {
if (loader == null) {
loader = ClassLoader.getSystemClassLoader();
}
if (LOAD_AS_RESOURCE_BUNDLE) {
name = name.replace('/', '.');
// Throws MissingResourceException on lookup failures:
final ResourceBundle rb = ResourceBundle.getBundle(name,
Locale.getDefault(), loader);
result = new Properties();
for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
final String key = keys.nextElement();
final String value = rb.getString(key);
result.put(key, value);
}
} else {
name = name.replace('.', '/');
if (!name.endsWith(SUFFIX)) {
name = name.concat(SUFFIX); // Returns null on lookup failures:
}
//logger.info(name);
in = loader.getResourceAsStream(name);
if (in != null) {
result = new Properties();
result.load(in); // Can throw IOException
}
}
} catch (Exception e) {
logger.log(Level.WARNING, "Error loading properties", e);
result = null;
} finally {
if (in != null) {
try {
in.close();
} catch (Throwable ignore) {
}
}
}
if (THROW_ON_LOAD_FAILURE && (result == null)) {
throw new IllegalArgumentException("could not load [" + name + "]" +
" as " + (LOAD_AS_RESOURCE_BUNDLE
? "a resource bundle"
: "a classloader resource"));
}
return result;
}
}
http://java.sun.com/j2se/1.4.2/docs/api/java/io/FilePermission.html
Please note: Code can always read a file from the same directory it's in (or a subdirectory of that directory); it does not need explicit permission to do so.
posted by sammyo at 5:58 PM on March 28, 2010