0

I'm trying to create a File object in order to save my properties file. I need to know how to specify a relative path from my package though because the below code does not work.

    try {
        File file = new File(new File(Thread.currentThread().getContextClassLoader().getResource("").toURI()), "com/configuration/settings.properties");
        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            properties.store(fileOutputStream, null);
        }
    } catch (IOException | URISyntaxException ioe) {
        System.out.println(ioe);
    }
  • 1
    Have you tried this? http://stackoverflow.com/questions/573679/open-resource-with-relative-path-in-java – Gary Dec 28 '12 at 13:56

2 Answers2

0

Relative path depends on current working directory from where you are running your program.

If you are running in IDE, IDE may set working directory path to Project directory. And How your program run depends on classpath to jar/claases in your program.

nullptr
  • 3,320
  • 7
  • 35
  • 68
0

Just replace this line:

File file = new File("/com/configuration/settings.properties");

with:

File file = new File(new File(Thread.currentThread().getContextClassLoader().getResource("").toURI()), "com/configuration/settings.properties");
tuga
  • 331
  • 2
  • 5