Skip to content

Instantly share code, notes, and snippets.

@amrishodiq
Created September 22, 2011 23:23
Show Gist options
  • Save amrishodiq/1236326 to your computer and use it in GitHub Desktop.
Save amrishodiq/1236326 to your computer and use it in GitHub Desktop.
Blackberry - method to read resource text file (in res directory)
/**
* I use this method to get contents of JSON formatted file that I put into res
* directory to deliver default data for my application.
* Visit my blog at http://web.durianapp.com
*/
public static String getResFileContent(String filename) {
InputStream is = null;
try {
if (klas == null) {
klas = Class.forName("com.durianapp.brandapp.BrandApp");
}
is = klas.getResourceAsStream(filename);
byte[] bytes = new byte[1024];
int len = 0;
StringBuffer buf = new StringBuffer();
while ((len = is.read(bytes)) != -1) {
buf.append(new String(bytes, 0, len));
}
return buf.toString();
} catch(Exception ex) {
System.out.println("Error: " + ex.toString());
return "";
} finally {
try {
if (is != null) is.close();
} catch (Exception e) {
System.out.println("Ex: "+e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment