Skip to content

Instantly share code, notes, and snippets.

@McGalanes
Created January 11, 2017 23:38
Show Gist options
  • Save McGalanes/569d7b64f5ee034156684f185ef2da67 to your computer and use it in GitHub Desktop.
Save McGalanes/569d7b64f5ee034156684f185ef2da67 to your computer and use it in GitHub Desktop.
public static class CacheUtils {
public static boolean exists(Context context, String filename) {
return new File(context.getCacheDir(), filename).exists();
}
public static void write(Context context, String filename, final byte[] content, final boolean append) {
final File cache = new File(context.getCacheDir(), filename);
try {
FileOutputStream out = new FileOutputStream(cache, append);
out.write(content);
out.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static String read(Context context, String filename) {
File cache = new File(context.getCacheDir(), filename);
if (cache.exists()) {
StringBuilder content = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(cache));
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
content.append('\n');
}
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
return String.valueOf(content);
}
} else {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment