0

I have an excel file in src/main/resources (/resources/files/templates). I want to read it to InputStream. I am not finding the right syntax for this.

String fn = "/resources/files/censusTemplates/SimpleStandardTemplate.xlsm";

    try {
        InputStream inputStream = new URL("file:///"  + fn).openStream();

Above is not working. I think the path is wrong. How to give correct path for files stored in resources?

Mohammed Shirhaan
  • 555
  • 1
  • 11
  • 27
  • What have you tried? Could you show us some code of your attempts? – n247s Jul 05 '19 at 11:23
  • 2
    Possible duplicate of [Read file from resources folder in Spring Boot](https://stackoverflow.com/questions/44399422/read-file-from-resources-folder-in-spring-boot) – Sergii Zhevzhyk Jul 05 '19 at 11:34

2 Answers2

0

This one works for me.

InputStream is = YourClass.class.getClassLoader().getResourceAsStream("relative path to your file starting from resources folder");
Mansur
  • 1,661
  • 3
  • 17
  • 41
0

If you are in any method (static or non-static):

String fn = "/resources/files/censusTemplates/SimpleStandardTemplate.xlsm";
InputStream in = Clazz.class.getResourceAsStream(fn);

Where Clazz is the name of the class you are in.


If you are in a non-static method:

String fn = "/resources/files/censusTemplates/SimpleStandardTemplate.xlsm";
InputStream in = getClass().getResourceAsStream(fn);
LBPFR34K
  • 56
  • 1
  • 8