Skip to content

Instantly share code, notes, and snippets.

@moneeb777
Last active April 29, 2016 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moneeb777/757d7238fdb5224546b2530cc0837dc2 to your computer and use it in GitHub Desktop.
Save moneeb777/757d7238fdb5224546b2530cc0837dc2 to your computer and use it in GitHub Desktop.
Android I/O- write file to storage
// find the root of the external storage
File root = android.os.Environment.getExternalStorageDirectory();
myRootFolder.append("\nExternal file system root: " + root);
//make a new directory
File dir = new File (root.getAbsolutePath() + "/testDownload");
dir.mkdirs();
File file = new File(dir, "testData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hello World");
pw.flush();
pw.close();
f.close(); //An error occurs if you do not implement a catch for this expression
} catch (Exception e) {
e.printStackTrace();
Log.i(LOG_TAG, "File not found. Probably a permissions issue");
}
myRootFolder.append("\n\nFile written to" + file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment