Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ask first yourself: Is your file an <strong>internal</strong> component of your application? (That usually implies that it's packed inside your JAR, or WAR if it is a web-app; typically, it's some configuration file or static resource, read-only). </p> <p>If the answer is yes, you don't want to specify an <strong>absolute path</strong> for the file. But you neither want to access it with a <strong>relative path</strong> (as your example), because Java assumes that path is relative to the "<strong>current directory</strong>". Usually the preferred way for this scenario is to load it <strong>relatively from the classpath</strong>.</p> <p>Java provides you the <em><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)" rel="noreferrer">classLoader.getResource()</a></em> method for doing this. And Eclipse (in the normal setup) assumes <code>src/</code> is to be in the root of your classpath, so that, after compiling, it copies everything to your output directory ( <code>bin/</code> ), the java files in compiled form ( <code>.class</code> ), the rest as is.</p> <p>So, for example, if you place your file in <code>src/Files/myfile.txt</code>, it will be copied at compile time to <code>bin/Files/myfile.txt</code> ; and, at runtime, <code>bin/</code> will be in (the root of) your classpath. So, by calling <code>getResource("/Files/myfile.txt")</code> (in some of its variants) you will be able to read it.</p> <p><em>Edited</em>: Further, if your file is conceptually tied to a java class (eg, some <code>com.example.MyClass</code> has a <code>MyClass.cfg</code> associated configuration file), you can use the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResource(java.lang.String)" rel="noreferrer">getResource() method from the class</a> and use a (resource) relative path: <code>MyClass.getResource("MyClass.cfg")</code>. The file then will be searched in the classpath, but with the class package pre-appended. So that, in this scenario, you'll typically place your <code>MyClass.cfg</code> and <code>MyClass.java</code> files in the same directory.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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