Reputation: 169
I have created a simple Maven project with no archetype, everything is okay. Then I added a CVA.pmml
file under the main/resources
directory. Afterwards, I want to read the file but get FileNotFoundException
. I tried the following methods:
Method 1:
InputStream is = BundleTest.class.getResourceAsStream("CVA.pmml");
Method 2:
InputStream is = BundleTest.class.getResourceAsStream("resources/CVA.pmml");
Method 3:
InputStream is = new FileInputStream("CVA.pmml");
Method 4:
InputStream is = new FileInputStream("resources/CVA.pmml");
None of them works. Any suggestion?
Here is the screenshot of the project structure:
Upvotes: 4
Views: 2624
Reputation: 691625
Method 1:
InputStream is = BundleTest.class.getResourceAsStream("CVA.pmml");
That will look for the CVA.pmml resource in the same package as the BundleTest class. But CVA.pmml is in the root package, whereas BundleTest is not.
Method 2:
InputStream is = BundleTest.class.getResourceAsStream("resources/CVA.pmml");
That will look for it in the resources package under the package of the BundleTest class. But it's in the root package.
Method 3:
InputStream is = new FileInputStream("CVA.pmml");
That wil look it on the file system, in the directory from which you executed the java command. But it's in the classpath, embedded in the jar (or in a classpath directory)
Method 4:
InputStream is = new FileInputStream("resources/CVA.pmml");
That wil look it on the file system, in the directory `resources, under the directory from which you executed the java command. But it's in the classpath, embedded in the jar (or in a classpath directory)
The correct way is
InputStream is = BundleTest.class.getResourceAsStream("/CVA.pmml");
(notice the leading slash)
or
InputStream is = BundleTest.class.getClassLoader().getResourceAsStream("CVA.pmml");
Upvotes: 5
Reputation: 11
Create a folder named static inside resources folder and put your files there . And access it like this "/CVA.pmml"
Upvotes: -2