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

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
Srujan Kumar Gulla
  • 5,721
  • 9
  • 48
  • 78

2 Answers2

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.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 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? – Srujan Kumar Gulla Apr 11 '13 at 15:31
  • You can determine your program's current directory using `new File("")`. Then construct all path's from that. – hmjd Apr 11 '13 at 15:39
  • sure. But can I change that reference point to some other directory which is more relevant? – Srujan Kumar Gulla Apr 11 '13 at 15:44
  • @srujangulla, do you mean change the current working directory of the program? If so, I don't think it is possible (also see http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java). – hmjd Apr 11 '13 at 15:45
1
File f = new File("config.properties");
String dirPath = file.getParentFile().getAbsolutePath()
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • 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? – Srujan Kumar Gulla Apr 11 '13 at 15:36