7

Is there any way of specifying the current directory in a java properties file?

i.e. something like:

fileLocation={currentDir}/fileName.txt 
1
  • What about "fileLocation=./fileName.txt", or just "fileLocation=fileName.txt"? Commented Jun 10, 2009 at 17:33

3 Answers 3

8

No. Properties files do not have any builtin macro facilities. You can programmatically get the currect directory of the user running the Java app through the user.dir system property.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Can you set the systems property in this way? For example, if I want to temporarily set the user.dir to some specific directory? I tried changing it before running a unit test, but it still wasn't able to load the file, even though the path returned from getProperty("user.dir") was correct....
No, the system property is set when the JVM is started, but later changes to it have no effect. I don't think it's possible to change the working directory of a running JVM.
3

I'm pretty sure it just defaults to the current directory, if not you can do

fileLocation="./fileName.txt" 

Comments

2

I don't know any direct solution for this problem. You can load the URL to the properties file and then load the filename from that file:

ClassLoader loader = YourClass.class.getClassLoader(); URL resourceURL =loader.getResource("check.properties"); String fileToLoad = resourceURL.getPath() + <fileNameFromPropertyFile>; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.