<p>How should I load files into my Java application?</p>

[{'answer_id': 6640, 'author': 'Will', 'author_id': 816, 'author_profile': 'https://Stackoverflow.com/users/816', 'pm_score': 6, 'selected': False, 'text': '<p><strong>The short answer</strong></p>\n\n<p>Use one of these two methods:</p>\n\n<ul>\n<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>\n<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>\n</ul>\n\n<p>For example:</p>\n\n<pre><code>InputStream inputStream = YourClass.class.getResourceAsStream("image.jpg");\n</code></pre>\n\n<p>--</p>\n\n<p><strong>The long answer</strong></p>\n\n<p>Typically, one would not want to load files using absolute paths. For example, don’t do this if you can help it:</p>\n\n<pre><code>File file = new File("C:\\\\Users\\\\Joe\\\\image.jpg");\n</code></pre>\n\n<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>\n\n<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>\n\n<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>\n'}, {'answer_id': 6679, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 3, 'selected': False, 'text': '<p>getResource is fine, but using relative paths will work just as well too, as long as you can control where your working directory is (which you usually can).</p>\n\n<p>Furthermore the platform dependence regarding the separator character can be gotten around using <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#separator" rel="noreferrer">File.separator</a>, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#separatorChar" rel="noreferrer">File.separatorChar</a>, or <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()" rel="noreferrer">System.getProperty("file.separator")</a>.</p>\n'}, {'answer_id': 6708, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 2, 'selected': False, 'text': '<p>I haven\'t had a problem just using Unix-style path separators, even on Windows (though it is good practice to check <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html#separatorChar" rel="nofollow noreferrer">File.separatorChar</a>).</p>\n<p>The technique of using <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#getResource(java.lang.String)" rel="nofollow noreferrer">ClassLoader.getResource()</a> is best for read-only resources that are going to be loaded from JAR files. Sometimes, <a href="https://illegalargumentexception.blogspot.com/2008/04/java-finding-application-directory.html" rel="nofollow noreferrer">you can programmatically determine the application directory</a>, which is useful for admin-configurable files or server applications. (Of course, user-editable files should be stored somewhere in the <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/System.html#getProperty(java.lang.String)" rel="nofollow noreferrer">System.getProperty(&quot;user.home&quot;)</a> directory.)</p>\n'}, {'answer_id': 30854, 'author': 'Vinnie', 'author_id': 2890, 'author_profile': 'https://Stackoverflow.com/users/2890', 'pm_score': 3, 'selected': False, 'text': '<p>What are you loading the files for - configuration or data (like an input file) or as a resource?</p>\n\n<ul>\n<li>If as a resource, follow the suggestion and example given by <a href="https://stackoverflow.com/questions/6639/how-should-i-load-files-into-my-java-application#6640">Will and Justin</a> </li>\n<li>If configuration, then you can use a <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html" rel="nofollow noreferrer">ResourceBundle</a> or <a href="http://www.springframework.org/" rel="nofollow noreferrer">Spring</a> (if your configuration is more complex).</li>\n<li>If you need to read a file in order to process the data inside, this code snippet may help <code>BufferedReader file = new BufferedReader(new FileReader(filename))</code> and then read each line of the file using <code>file.readLine();</code>  Don\'t forget to close the file.</li>\n</ul>\n'}, {'answer_id': 5058626, 'author': 'cibercitizen1', 'author_id': 286335, 'author_profile': 'https://Stackoverflow.com/users/286335', 'pm_score': 2, 'selected': False, 'text': '<pre><code>public byte[] loadBinaryFile (String name) {\n    try {\n\n        DataInputStream dis = new DataInputStream(new FileInputStream(name));\n        byte[] theBytes = new byte[dis.available()];\n        dis.read(theBytes, 0, dis.available());\n        dis.close();\n        return theBytes;\n    } catch (IOException ex) {\n    }\n    return null;\n} // ()\n</code></pre>\n'}, {'answer_id': 59455279, 'author': 'Fridjato Part Fridjat', 'author_id': 12087120, 'author_profile': 'https://Stackoverflow.com/users/12087120', 'pm_score': 0, 'selected': False, 'text': '<pre><code>public static String loadTextFile(File f) {\n    try {\n        BufferedReader r = new BufferedReader(new FileReader(f));\n        StringWriter w = new StringWriter();\n\n        try {\n            String line = reader.readLine();\n            while (null != line) {\n                w.append(line).append("\\n");\n                line = r.readLine();\n            }\n\n            return w.toString();\n        } finally {\n            r.close();\n            w.close();\n        }\n    } catch (Exception ex) {\n        ex.printStackTrace();\n\n        return "";\n    }\n}\n</code></pre>\n'}, {'answer_id': 67989705, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>use docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#getResource(java.lang.String)</p>\n'}]
['https://Stackoverflow.com/questions/6639', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/816/']