Rafael Borja
Rafael Borja

Reputation: 4917

How to read a file deployed within an EAR

How can I read a file deployed as a resource within a EAR or a WAR?

I want to create a new FileInputStream calling the constructor below.

java.io.FileInputStream.FileInputStream(String)

But the file I want to read is located on path "/glm/src/main/resources/excel_templates" inside the WAR file.

Is it possible to create a FileInputStream with a EAR or WAR based filepath?

Upvotes: 1

Views: 7873

Answers (1)

Shivan Dragon
Shivan Dragon

Reputation: 15229

Try something like this:

URL url = SomeClassInEar.getClass().getClassLoader().getResource("rootDirInEar/SubDir/fileInEar.whatever");
File file = new File(url.getPath());
FileInputStream fis = new FileInputStream(file);

Upvotes: 3

Related Questions