Search code examples
javaliferayinputstreamliferay-6fileinputstream

Issue with file path in Java/Liferay


I have tried in multiple ways to load the property file from the resource folder.

Every time, I'm getting a file not found exception. My code is as follows:

 Properties prop = new Properties();
        FileInputStream inputStream = new FileInputStream("/resource/excelfilepath.properties");
        prop.load(inputStream);
        String path = prop.getProperty("excelPath");
        System.out.println("Excel File Path "+ path);

My project structure looks as follows,

enter image description here

What is the needed structure of the file path literal?


Solution

  • I don't think that you really want to read a ....properties file from web resources. That way the content is visible to all users that access your server - as long as you don't hide it explicitly in web.xml.

    It's much more common to put it into the classpath next to your accessing class. That way you can access it with the classloader and it is not visible to the webusers anymore:

    Properties prop = new Properties();
    prop.load(CreateUser.class.getResourceAsStream("excelfilepath.properties"));
    

    But as you are using Liferay, you should use its configuration as well. Just add the property UserCreationPortlet.excelPath to your portal-ext.properties and use:

    String path = PrefsPropsUtil.getString("UserCreationPortlet.excelPath", defaultPath);