Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 22:57, 9 November 2013 by lpvalente (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to read a text file from JAR

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: firt (15 Apr 2007)
Last edited: lpvalente (09 Nov 2013)

This article demonstrates how to read a text file from a 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());
}
This page was last modified on 9 November 2013, at 22:57.
53 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×