tags:

views:

594

answers:

4

In Ruby I frequently use File.dirname(__FILE__) to open configuration files and such. For those that don't know Ruby, this will give the location on the file system of the file it's called from. This allows me to package libraries with data and config files and open those files with relative paths.

What's the Java equivalent of this? If there is a data file I want to package with a jar how would I open the data file from Java code that is also in the jar?

+1  A: 

Please see Java Applications and the "Current Directory":

In Java, you use File objects to construct a relative view of the file system. Two of the constructors for the File object take a 'parent' argument that specifies a parent path that is prefixed to the path of the file itself to create the full abstract path to the file. What you do is, create a File object with the path that represents your current directory and then create all your file objects using that File object as the parent. Voila, a current directory.

Also I would recommend Reading and Writing a Properties File:

// Read properties file.
Properties properties = new Properties();
try {
    properties.load(new FileInputStream("filename.properties"));
} catch (IOException e) {
}

// Write properties file.
try {
    properties.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) {
}
Andrew Hare
I don't think this addresses the original question. The OP was asking how to get other files packaged with the classfile being executed: which would be on the classpath (may be in a directory or in a jarfile). This solution accesses a file in the current user directory: the directory the program was run from, which is unrelated to classpath.
Chadwick
+7  A: 

The equivalent API in Java is getResourceAsStream. This will open a stream to a file stored in the JAR relative to the class on which it is invoked.

There are variants, such as getResource, which returns a URL, or methods on ClassLoader that use an absolute path inside the JAR file.

erickson
Thanks, that's what I was looking for. One follow up question. While developing it seems to expect the resource file to be in the folders that the class files go into. I don't keep these folders under version control but the resource needs to be. How do is this normally handled? Does your build script (ant or whatever) copy the file from a vc'd place to the build folders?
jshen
I keep class resources in version control alongside the Java source files, then my build copies them into the corresponding directories after classes are compiled. Then those directories are zipped up in a JAR.
erickson
thanks for all the help :)
jshen
getResourceAsStream will get files from anywhere on the class path which can include JARs and directories so while the code comes from a JAR you can have the files come from a directory on the file system. (Or any URL like a web server)
Peter Lawrey
A: 

I voted for @andrew, but I'd like to point out that in Java, the directory the file itself is pretty meaningless except for a few highly reflective (and most likely wrong) purposes.

It will be in a directory based on a package structure, and could be located in a jar or pretty much anywhere.

Java's a little less ad-hock, allowing for less shortcuts, but usually improving consistency and reliability at deployment time.

Bill K
ruby's File.expand_path(File.dirname(__FILE__)) is consistent, reliable and easy.
jshen
A: 

if you just specify the file name, the JVM will look for that file in the same directory your application was started (not necessarily the same directory your application is located).

cd1
That's often called the "current working directory", by the way.
erickson