tags:

views:

43

answers:

1

Let's say I have a structure

-bin/com/abc/A.class
-src/com/abc/A.java
-config/info.txt

How to address the file info.txt from A class? Should we use "user.dir" property or "config/info.txt" so that it would work ? I'll compile this into the jar and after that the jar will be used from the servlet, but I don't think that's important cause this file is written and read from internal jar's methods only.

+1  A: 

Just put it in the runtime classpath and use ClassLoader#getResourceAsStream() to get an InputStream of it. Putting it in the JAR file among the classes, or adding its (JAR-relative) path to the Class-Path entry of the JAR's manifest.mf file is more than sufficient.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("config/info.txt");
// Do your thing to read it.

Or if you actually want to get it in flavor of a java.io.File, then make use of ClassLoader#getResource(), URL#toURI() and the File constructor taking an URI:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("config/info.txt");
File file = new File(url.toURI());
// Do your thing with it.

Do not use relative paths in java.io stuff. It would be dependent on the current working directory which you have no control over at any way. It's simply receipt for portability trouble. Just make use of the classpath.

That said, are you aware of the java.util.Properties API? It namely look like you're trying to achieve the same thing which is more easy to be done with propertiesfiles.

BalusC
@BalusC I maybe was not very clear about the whole thing. What you're advising is great thing, but I need a simpler solution. The file will be kept in the jar, and I don't need [now, I will maybe need later, so thanks anyway] - I don't need to extract the file in the outer project. I just need to read a project's own folder and it's content, ie a file. What would you say? "user.dir" or "./" ?
EugeneP
It can't be done simpler. You would then be entirely dependent on the underlying platform and on the way how the program is executed. The JAR file itself is already in the runtime classpath. You can also just put the file relative to it and use the aforesuggested way. It isn't that hard. I can however imagine that starters have trouble understanding the phenomenon "classpath" and how to take benefit of it. Good luck.
BalusC
Or, if your **actual** question is: "how do I get a worthfully `File` object of it?" (then please ask so), you can get it by `new File(URI)` where the `URI` is obtained by `ClassLoader#getResource()` and then `URL#toURI()`.
BalusC
@BalusC I don't get it. What if I just do it that way File("config/props.properties") ?
EugeneP
If I wanna be sure that it must work under one platform ie Linux.
EugeneP
Relative paths in Java IO are dependent on the current working directory. You really won't let your code depend on stuff which you have no control over. Just put file in classpath and use classloader. That's all. It isn't hard and it works. If you have problems with getting it to work, then elaborate so instead of asking how to achieve poor workarounds.
BalusC