Created
November 17, 2015 12:39
-
-
Save walteranyika/5a725a24aaf7e60864f2 to your computer and use it in GitHub Desktop.
CODE TO SAVE AND READ FROM TEXT FILES
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 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