-1

I created a simple maven project. I placed one properties file and one xlsx file in src/main/resources folder. How can i load the properties/excel files? I tried as below.

InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("/app.properties");
            props.load(inputStream);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

It throws exception saying that properties file could not be located. Am i doing anything wrong?

Thanks!

user755806
  • 6,565
  • 27
  • 106
  • 153

2 Answers2

1

Take help of ServletContext's getResource() and getResourceAsStream(). Suppose you have the following folder structure src/resources/images

    String file1 = "images/filling.png";
    InputStream stream= ClassName.class.getClassLoader().getResourceAsStream(file1);

   String file2= "images/filling.png";
   InputStream stream= ClassName.class.getClassLoader().getResource(file2);
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • That will not work, cause the folder `src/main/resources` is automatically copied to target/classes which can be used by `getResourcesAsStream()`. So the part `resources` is wrong here. – khmarbaise Jun 19 '14 at 09:59
  • If under resources image is a subfolder then String file1 = "images/filling.png"; InputStream stream= ClassName.class.getClassLoader().getResourceAsStream(file1); thats what you mean right?? @khmarbaise – SparkOn Jun 19 '14 at 10:22
0

In your servlet, you can get absolute path using getServletContext().getRealPath ("."), this will return you the absolute path, in it append remaining path till your resources folder and read the desired files using FileInputStream.

Programmer
  • 325
  • 5
  • 18