Skip to content

Instantly share code, notes, and snippets.

@anta40
Forked from amrishodiq/StringUtilities.java
Last active August 29, 2015 14:06
Show Gist options
  • Save anta40/1c24d0dc724ae63f10a3 to your computer and use it in GitHub Desktop.
Save anta40/1c24d0dc724ae63f10a3 to your computer and use it in GitHub Desktop.
/**
* 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