How to read a text file from JAR
Article Metadata
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());
}


13 Sep
2009
It shows Basic theory for reading data from files through jar files in a very easy way.
File Manipulation is the basic thing and this article helps me to understand it.So If anyone wants to use data from web server then this concept helps because web server holds txt,csv files.
and the web address of that file can be input of the given function.So It makes easy data Retrieval and data manipulation.
Would be good if someone provides an example for a valid file name with valid scheme, like: jar://jar-file.jar!enclosed-file.txt