I want to read a file in the root file of the jar file. The code is listed as following. The method I used is the create an File object to generate the instance of InputStream.
The content of the text file:
It worked perfectly when I run it in Eclipse, but it didn't work in a jar file. It threw a IOException when I run the jar file. I have already included the test.txt file in the root path of jar file. I know I can make it work just copy another test.txt file and put it in the same directory with the jar file so that the jar file can read the file. But my purpose is I don't want every user to have a copy of test.txt and put it in the same path with jar file. Anyone can help?
Or replace the first line with this:
That is, if the file is located in the root of your JAR file. If it's located in the same folder as your class file simply use getClass().getResourceAsStream("test.txt").