Skip to content

Instantly share code, notes, and snippets.

@alvindizon
Last active May 12, 2020 08:39
Show Gist options
  • Save alvindizon/e15d4bf3a15d0b7f8cb2a5a7cacacc1c to your computer and use it in GitHub Desktop.
Save alvindizon/e15d4bf3a15d0b7f8cb2a5a7cacacc1c to your computer and use it in GitHub Desktop.
write text file to sdcard folder in android
new Thread(() -> {
String errorPath = Environment.getExternalStorageDirectory().getPath() +
File.separator + Const.TEST_FILE_FOLDER;
File exceptionFolder = new File(errorPath);
if(!exceptionFolder.exists()) {
exceptionFolder.mkdir();
}
File errFile = new File(errorPath + File.separator + Const.TEST_FILE);
if(!errFile.exists()) {
try {
errFile.createNewFile();
} catch (IOException e) {
Toast.makeText(context, "Error creating test file", Toast.LENGTH_SHORT).show();
return;
}
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter(errFile, true))) {
bw.append(value);
bw.newLine();
} catch (Exception e) {
Toast.makeText(context, "Error writing to file", Toast.LENGTH_SHORT).show();
}
}).start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment