How to read file from JAR file (classpath)

But how to read a file from classpath? Open it with getResourceAsStream method and if you want to read text file, use InputStreamReader for conversion to Reader.

This way you can read a file from your harddrive in Java. But how to read a file from classpath? Which means from current JAR or WAR or any other JAR this application uses. You must open your file using:

InputStreamReader inputReader 
= new InputStreamReader(Main.class.getClass()

If your file is inside some package, use slash do delimit packages. This method returns InputStream. If you want to open a text file, convert it to reader using InputStreamReader.

comments powered by Disqus


Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects: