thieumao

Write txt file in Android

Apr 21st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1.         // get the path to sdcard
  2.         File sdcard = Environment.getExternalStorageDirectory();
  3.         // to this path add a new directory path
  4.         File dir = new File(sdcard.getAbsolutePath() + "/thieumao/");
  5.         // create this directory if not already created
  6.         dir.mkdir();
  7.         // create the file in which we will write the contents
  8.         File file = new File(dir, "name.txt");
  9.         FileOutputStream os;
  10.         try {
  11.             os = new FileOutputStream(file);
  12.             String data = "Messages";
  13.             try {
  14.                 os.write(data.getBytes());
  15.             } catch (IOException e) {
  16.                 e.printStackTrace();
  17.             }
  18.             try {
  19.                 os.close();
  20.             } catch (IOException e) {
  21.                 e.printStackTrace();
  22.             }
  23.         } catch (FileNotFoundException e) {
  24.             e.printStackTrace();
  25.         }
Advertisement
Add Comment
Please, Sign In to add comment