

I suggest using this method on the ClassLoader instance. Use the getResource() method to get the File instance when reading a file from inside a war file. Reading file from resources folder packaged as. Now, run the main() method from the console. ReadFileFromResourcesUsingGetResourceAsStream Also, provide the mainClass attribute to maven-jar-plugin and set its value to the class which has main() method and the test code. To test the above code, package the application as jar file using mvn clean package command.

Try (InputStreamReader isr = new InputStreamReader(is) īufferedReader br = new BufferedReader(isr) ) Private void printFileContent(InputStream is) throws IOException Is = instance.getFileAsIOStream("data/demo.txt") InputStream is = instance.getFileAsIOStream("demo.txt") = new ReadFileFromResourcesUsingGetResourceAsStream() ReadFileFromResourcesUsingGetResourceAsStream instance Creating instance to avoid static member methods Public static void main(final String args) throws IOException Public class ReadFileFromResourcesUsingGetResourceAsStream Once we have the InputStream reference, we can use it to read the file content or pass it to any resource handler class.

Throw new IllegalArgumentException(fileName + " is not found") Private InputStream getFileAsIOStream(final String fileName) The second file data/demo.txt folder is inside a nested folder data in the resources folder. The first file demo.txt is at the root of resources folder. In the given examples, we are reading two files present in the resources folder. We can refer to this file using File instance and can use any suitable method to read the file content.
JAVA INPUTSTREAM FILE PATH ARCHIVE
So, in this case, we are reading the file outside a zipped archive so we can refer the file using relative path.
