How to read a text file from JAR
If you want to read a text file from JAR file (read only), you have to:
- Include the txt file in your project, so the IDE can append it to the JAR package.
- Use the following code to read a text file from the JAR
private String readTextFile(String fileName) throws IOException {
InputStream input = getClass().getResourceAsStream(fileName);
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[512];
int bytes;
while ((bytes = input.read (buffer)) > 0) {
output.write (buffer, 0, bytes);
}
input.close ();
return new String(output.toByteArray());
}

