Namespaces
Variants
Actions

How to read a text file from JAR

Jump to: navigation, search
SignpostIcon FloppyDisk 52.png
Article Metadata

Article
Created: firt (15 Apr 2007)
Last edited: hamishwillee (11 Jul 2012)

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());
}
This page was last modified on 11 July 2012, at 08:23.
132 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved