Maven exec:java : how to open and read a file in the resources directory?
I guess I will answer my own question, Thread.currentThread().getContextClassLoader().getResourceAsStream()
works the best for me, especially when the project produces a jar dependency for another web project.
Figure I'd add to the answers.
You can also use:
InputStream file = ClassLoader.getSystemResourceAsStream("res.txt");
Try
InputStream IS = Main.class.getResourceAsStream("res.txt");
to access the content of res.txt
. Pay attention to the encoding of your text file (beware of defaults). If your maven project is set on UTF-8 for example, make sure res.txt
is encoded in UTF-8 too, otherwise, you'll get funny errors at runtime.