0
votes

We are trying to read a property file in a servlet using fileInputStream.

However we are constanlty getting a file not found exception.

This is the piece of code we are using

Properties properties = new Properties();
          File propertyFile = new File("config" + File.separatorChar + "abc.properties");
          try {
          FileInputStream propertyFileStream = new FileInputStream(propertyFile);
                properties.load(propertyFileStream);
                propertyFileStream.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

While using getResourceAsStream it is working fine.

However we need to understand why FileInputStream is not working.

We have placed the config\abc.properties file in the webInf. We have also tried placing it in the src folder(java classpath), the webContent folder, the WebInf\Classes folder but no success.

3
not working? your are not able to get property values? - KhAn SaAb
It is giving FileNotFOundException - user123
and i am calling it in a web application - user123
is it looking for some absolute path? what does the error print? - sidgate
This is the error. java.io.FileNotFoundException: config\abc.properties (The system cannot find the path specified) - user123

3 Answers

0
votes

Resources are not files. They don't live in the file system and they cannot be accessed via File or FileInputStream.

You should be using Class.getResource().

0
votes

Try by using

  ResourceBundle resource = ResourceBundle.getBundle("test");
  String VALUE1=resource.getString("KEY1");
  String VALUE2=resource.getString("KEY2");
0
votes

You should use this code to get resources on web application, because the path must be taken from ServletContext, I think that's what you're looking for, if you are inside Servlet:

InputStream is = getContext().getResourceAsStream("/WEB-INF/yourFolder/abc.properties");

to get the full path for your interest:

String fullPath = getContext().getRealPath("/WEB-INF/yourFolder/abc.properties");