Skip to content

Instantly share code, notes, and snippets.

@walteranyika
Created November 17, 2015 12:39
Show Gist options
  • Save walteranyika/5a725a24aaf7e60864f2 to your computer and use it in GitHub Desktop.
Save walteranyika/5a725a24aaf7e60864f2 to your computer and use it in GitHub Desktop.
CODE TO SAVE AND READ FROM TEXT FILES
public static void writeStringAsFile(final String fileContents, String fileName) {
Context context = App.instance.getApplicationContext();
try {
FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName));
out.write(fileContents);
out.close();
} catch (IOException e) {
Logger.logError(TAG, e);
}
}
public static String readFileAsString(String fileName) {
Context context = App.instance.getApplicationContext();
StringBuilder stringBuilder = new StringBuilder();
String line;
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
while ((line = in.readLine()) != null) stringBuilder.append(line);
} catch (FileNotFoundException e) {
Logger.logError(TAG, e);
} catch (IOException e) {
Logger.logError(TAG, e);
}
return stringBuilder.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment