Resource Loading

Use either of below way given a) directory is on the classpath. b) class loading the resource is loaded by the same classloader.

1.    From ClassLoader perspective, all paths are “absolute” already – there’s no context from which they could be relative. Therefore, no need for a leading slash.
InputStream in = this.getClass().getClassLoader().getResourceAsStream(“SomeTextFile.txt”);

2.    From Class perspective, the path is relative to the package of the class unless a leading slash is included, so if we don’t want to use the current package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream(“/SomeTextFile.txt”);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s