Created
January 11, 2017 23:38
-
-
Save McGalanes/569d7b64f5ee034156684f185ef2da67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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