Srujan Kumar Gulla
Srujan Kumar Gulla

Reputation: 5841

Finding absolute path

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

Upvotes: 1

Views: 5882

Answers (2)

Achintya Jha
Achintya Jha

Reputation: 12843

File f = new File("config.properties");
String dirPath = file.getParentFile().getAbsolutePath()

Upvotes: 1

hmjd
hmjd

Reputation: 121971

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.

Upvotes: 7

Related Questions