1

I am looking at code new FileInputStream("config.properties").

I have the same file "config.properties" in multiple places in my project(doing windows file search) and I am now confused as to which one does this function call refer to. How do i get to know the absolute path of file?

I found this on the internet but this location doesnt look like the right answer.

"ClassName".class.getProtectionDomain().getCodeSource().getLocation().getPath() but this doesnt look it. Can you please correct it if I am wrong

2
  • use File file = new File("config.properties");file.getAbsolutePath(); Commented Apr 11, 2013 at 15:30
  • Right. I was looking at FileInputStream class and tried if it had any method to find the absolute path but i can just pass the path name to File class and then find the answer. Commented Apr 11, 2013 at 15:37

2 Answers 2

7

You can use File:

File f = new File("config.properties"); System.out.println(f.getAbsolutePath()); 

The path returned will be deduced from the current working directory.

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

4 Comments

thanks. Is there any way I can change and set a relative path for my program and start referencing other directories using that directory as base?
You can determine your program's current directory using new File(""). Then construct all path's from that.
sure. But can I change that reference point to some other directory which is more relevant?
@srujangulla, do you mean change the current working directory of the program? If so, I don't think it is possible (also see stackoverflow.com/questions/840190/…).
1
File f = new File("config.properties"); String dirPath = file.getParentFile().getAbsolutePath() 

1 Comment

thanks. Is there any way I can change and set a relative path for my program and start referencing other directories using that directory as base?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.