Question

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

Was it helpful?

Solution

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.

OTHER TIPS

File f = new File("config.properties");
String dirPath = file.getParentFile().getAbsolutePath()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top