Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are specifying resource files with paths. That only works if you actually know the full path or the relative path from current directory; e.g. prepended with "src" as suggested in another answer.</p> <p>It is, however, much better to include resources in your build (whether you build through your IDE or an external build system like gradle or maven) so that these files are copied into the classpath (it may be that you're already doing this). In this case, you can retrieve the resources using the Java classloader, like this:</p> <pre><code>URL resource = getClass().getResource("/path/to/resource"); </code></pre> <p>A couple of notes here: </p> <ol> <li>The leading "/" specifies that the classloader should search from the root package. If it is ommitted, it will search relative from the package of the class you're in.</li> <li>You're getting a URL instead of a File. Quite often, it is possible to use these URL's directly, like in the <code>ImageIcon</code> contructor that you're using. One additional benefit with this method is that it works if your application is packaged in jar file as well, since then the URL will point into the correct location in the jar file. However, if you <em>know</em> it is a file, <em>and</em> you need the file reference, it can be converted like this: <code>File file = new File(resource.toURI());</code>. </li> </ol> <p>EDIT: The following is an example that should work for Danny, based on his "answer" post with an example.</p> <pre><code>import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; public class ResourceTest { public static void main(String[] args) throws IOException, URISyntaxException { new ResourceTest().test(); } public void test() throws URISyntaxException, IOException { URL url = getClass().getClassLoader().getResource("files/1.txt"); System.out.println("URL: " + url); File file = new File(url.toURI()); System.out.println("FILE: " + file); File folder = file.getParentFile(); System.out.println("FOLDER: " + folder); System.out.println("FOLDER LIST: "); for (String fileName : folder.list()) { System.out.println(" " + fileName); } System.out.println("FILE CONTENTS:"); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); } } } </code></pre> <p>Which for yields the following output:</p> <pre><code>URL: file:/home/steinar/Safe/source/private/out/production/files/1.txt FILE: /home/steinar/Safe/source/private/out/production/files/1.txt FOLDER: /home/steinar/Safe/source/private/out/production/files FOLDER LIST: 1.txt FILE CONTENTS: This is my test text file </code></pre>
    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. This table or related slice is empty.
    1. 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