madhusudhan
madhusudhan

Reputation: 400

Read file from resource folder always giving Inputstream is null in spring boot application

Always giving Inputstream is null in spring boot application for me. Below code for getting inputstream object and image is for my resource file location. Why I am getting inputstream object is null I am not sure.

Is there any issue I am loading this file while running power mock test case

IDataSet dataSet;
InputStream is = ClassLoader.class.getResourceAsStream("/all/testdata.json");
if (dataScript.endsWith(".json")) {
   dataSet = new JsonDataSet(is);
}

[1]: https://i.sstatic.net/kQxug.png

Upvotes: 0

Views: 1108

Answers (2)

Moritz Petersen
Moritz Petersen

Reputation: 13047

Use the system classloader to access resources from the root:

ClassLoader.getSystemResourceAsStream("/all/testdata.json");

Upvotes: 2

Simon Martinelli
Simon Martinelli

Reputation: 36103

If you use:

InputStream is = YourClass.class.getResourceAsStream("/all/testdata.json");

Java looks relative to YourClass for the given file path.

If you use:

InputStream is = YourClass.class.getClassLoader().getResourceAsStream("/all/testdata.json");

Java looks relative from the root of the class path.

So I assume that you are looking for the second option

Upvotes: 0

Related Questions