Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The short answer</strong></p> <p>Use one of these two methods:</p> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResource(java.lang.String)" rel="noreferrer"><code>Class.getResource(String)</code></a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)" rel="noreferrer"><code>Class.getResourceAsStream(String)</code></a></li> </ul> <p>For example:</p> <pre><code>InputStream inputStream = YourClass.class.getResourceAsStream("image.jpg"); </code></pre> <p>--</p> <p><strong>The long answer</strong></p> <p>Typically, one would not want to load files using absolute paths. For example, don’t do this if you can help it:</p> <pre><code>File file = new File("C:\\Users\\Joe\\image.jpg"); </code></pre> <p>This technique is not recommended for at least two reasons. First, it creates a dependency on a particular operating system, which prevents the application from easily moving to another operating system. One of Java’s main benefits is the ability to run the same bytecode on many different platforms. Using an absolute path like this makes the code much less portable.</p> <p>Second, depending on the relative location of the file, this technique might create an external dependency and limit the application’s mobility. If the file exists outside the application’s current directory, this creates an external dependency and one would have to be aware of the dependency in order to move the application to another machine (error prone).</p> <p>Instead, use the <code>getResource()</code> methods in the <code>Class</code> class. This makes the application much more portable. It can be moved to different platforms, machines, or directories and still function correctly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload