Sign up ×

How should I load files into my Java application?

share|improve this question
If you are using Maven and if your file contains text, you need to pay attention to the encoding (see here:…) –  JVerstry Jul 27 '12 at 19:25

5 Answers 5

The short answer

Use one of these two methods:

For example:

InputStream inputStream = 


The long answer

Typically, one would not want to load files using absolute paths. For example, don’t do this if you can help it:

File file = new File("C:\\Users\\Joe\\image.jpg");

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.

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).

Instead, use the getResource() methods in the Class class. This makes the application much more portable. It can be moved to different platforms, machines, or directories and still function correctly.

share|improve this answer
Esp. don't do File(“C:\Users\Joe\image.jpg”) because the \U \J and \i are all escapes and you'll get an error. <smile> –  Lawrence Dol May 2 '09 at 7:38
Can also use it like this: getClass().getClassLoader().getResource("image.jpg"); –  ktulinho May 31 '13 at 13:45

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).

Furthermore the platform dependence regarding the separator character can be gotten around using File.separator, File.separatorChar, or System.getProperty("file.separator").

share|improve this answer… says: The name of a resource is a '/'-separated path name that identifies the resource. So don't use File.separator. –  Vsevolod Golovanov Aug 18 at 11:17

What are you loading the files for - configuration or data (like an input file) or as a resource?

  • If as a resource, follow the suggestion and example given by Will and Justin
  • If configuration, then you can use a ResourceBundle or Spring (if your configuration is more complex).
  • If you need to read a file in order to process the data inside, this code snippet may help BufferedReader file = new BufferedReader(new FileReader(filename)) and then read each line of the file using file.readLine(); Don't forget to close the file.
share|improve this answer
public byte[] loadBinaryFile (String name) {
    try {

        DataInputStream dis = new DataInputStream(new FileInputStream(name));
        byte[] theBytes = new byte[dis.available()];, 0, dis.available());
        return theBytes;
    } catch (IOException ex) {
    return null;
} // ()
share|improve this answer
This uses the available() method to a) allocate and b) determine the number of bytes to read from the stream. This should never be done since the method is not supposed to return the total number of bytes in the stream. See the javadoc: "It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream." –  f1sh Apr 29 '14 at 14:46

I haven't had a problem just using unix-style path separators, even on Windows (though it is good practice to check File.separatorChar).

The technique of using ClassLoader.getResource() is best for read-only resources that are going to be loaded from JAR files. Sometimes, you can programmatically determine the application directory, which is useful for admin-configurable files or server applications. (Of course, user-editable files should be stored somewhere in the System.getProperty("user.home") directory.)

share|improve this answer
In practice using forward slashes works on every Java platform (and I have used them Windows 2000, XP, Vista, Mobile, CE, Linux, OSX, OS/400). –  Lawrence Dol May 2 '09 at 7:41

protected by Marco A. Nov 8 '14 at 17:58


Would you like to answer one of these unanswered questions instead?
