Advertisement
rraito

Read and write dan beberapa fungsi file di android

Apr 2nd, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. Read and write file di android
  2.  
  3. -READING-
  4.  
  5. pertama tentuin lokasi file yang kamu mau
  6. String file_location = "/sdcard/myid.txt";
  7.  
  8. lalu panggil method file di android
  9. File maijangar = new File(file_location);
  10.  
  11. untuk membaca file maka gunakan FIleInputStream
  12. String aBuffer = "";
  13. FileInputStream fIn = null;
  14. try {
  15. fIn = new FileInputStream(myFile);
  16. } catch (FileNotFoundException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. Setela itu seperti biasa masukin ke BeufferdReader
  21. BufferedReader myReader = new BufferedReader(new InputStreamReader(
  22. fIn));
  23. dan dibaca untuk setiap barisnya
  24. String aDataRow = "";
  25.  
  26. try {
  27. while ((aDataRow = myReader.readLine()) != null) {
  28. aBuffer += aDataRow;
  29. }
  30. } catch (IOException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. dan Jangan lupa untuk di tutup
  35. try {
  36. myReader.close();
  37. } catch (IOException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. Log.d("readFile : ", aBuffer);
  42. return aBuffer;
  43. -===============================================================-
  44. Membuat File Baru
  45.  
  46. tinggal create new File Aja pada kelas File
  47. try {
  48. myFile.createNewFile();
  49. } catch (IOException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53.  
  54. -===============================================================-
  55. Membuang File
  56.  
  57. tinggal panggil delete aja di kelas File
  58. myFile.delete()
  59. -===============================================================-
  60. -WRITING-
  61. yang di gunain yaitu FileOutputStream yang sebelumnya dimasukkan ke myFile tentunya
  62. FileOutputStream fOut = null;
  63. try {
  64. fOut = new FileOutputStream(myFile);
  65. } catch (FileNotFoundException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69.  
  70. Nah setelah itu masukin teks dari dokumen yang ada pada FileOutputStream dimasukkan ke OutputStreamWriter, lalu di append berdasarkan teks yang diminta :
  71. OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
  72. try {
  73. myOutWriter.append(myText);
  74. } catch (IOException e) {
  75. // TODO Auto-generated catch block
  76. e.printStackTrace();
  77. }
  78.  
  79. Jangan lupa di Tutup pastinya
  80.  
  81. try {
  82. myOutWriter.close();
  83. } catch (IOException e) {
  84. // TODO Auto-generated catch block
  85. e.printStackTrace();
  86. }
  87. try {
  88. fOut.close();
  89. } catch (IOException e) {
  90. // TODO Auto-generated catch block
  91. e.printStackTrace();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement