2

I am working on a SpringMVC project which runs a number of automated tests on a database. The access details for this database are located in a .properties file. This file is located within the project directory.

 FileInputStream fis = new FileInputStream("batch-dm.properties");
    propFile = new Properties();
    propFile.load(fis);

As the file is stored in the project directory the FileInputStream should be able to access it no? If I provide the absolute path e.g.

FileInputStream fis = new FileInputStream("C:/workspace/Project/batch-dm.properties");

It recognises the file and runs properly. Do I need to sore this file in a different location because it is a Spring MVC project? Thanks

RichardMcK
  • 35
  • 1
  • 8

2 Answers2

0

Please refer below link:
https://stackoverflow.com/a/2308388/1358551

You may have to use getResourceAsStream().

Community
  • 1
  • 1
charybr
  • 1,888
  • 24
  • 29
0

Just to clear out your mind, try to see what is the value that outputs System.getProperty("user.dir") (let's call it A), this will print the complete absolute path from where your application was initialized, more specifically it will print the directory from where the JVM was started.

If you doesn't supply a parent path to the file that you are trying to open, the (A) path is taken by default and the file is looked inside that directory. So please, have in mind that.


Aditional information

If you absolutely need that file you should include it in your project so you can access it as a resource. Resource is a file that is included in your project and came bundled with the generated .jar or .war for re distribution.

My advice is to put the file in the package and use as a resource as it the safer way to work with external resources that should be shipped with your project.

Take a deeper look at this post for more about practical way of handling resources.

Community
  • 1
  • 1
Victor
  • 3,841
  • 2
  • 37
  • 63
  • Thanks, I fixed the problem by adding the file to the resources directory and using `this.class.getClassLoader().getResourceAsStream("/file.properties");` to get it as an InputStream instead of a FileInputStream. – RichardMcK Jun 10 '15 at 16:40
  • Glad to help Dude! :D – Victor Jun 10 '15 at 19:52