Reading and Writing a file to SD card sample program in Android

By: Ashley Viewed: 200 times  Printer Friendly Format    


This sample android program shows you how write and read a file from SD Card in Android. In this program four buttons are shown and a Edit box. When you type some text into the edit box and click, Save to SD Card button, the text is saved to a text file and saved to the SD Card. When you click clear button, the edit box contents are cleared. When you click, Read Sd card button the file is read from the SD card and the contents are copied to the edit box. 

The FileDemo2.java file is as follows:

package com.javasamples;
import java.io.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class FileDemo2 extends Activity {
	// GUI controls
	EditText txtData;
	Button btnWriteSDFile;
	Button btnReadSDFile;
	Button btnClearScreen;
	Button btnClose;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	// bind GUI elements with local controls
	txtData = (EditText) findViewById(R.id.txtData);
	txtData.setHint("Enter some lines of data here...");

	btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
	btnWriteSDFile.setOnClickListener(new OnClickListener() {

	public void onClick(View v) {
		// write on SD card file data in the text box
		try {
			File myFile = new File("/sdcard/mysdfile.txt");
			myFile.createNewFile();
			FileOutputStream fOut = new FileOutputStream(myFile);
			OutputStreamWriter myOutWriter = 
									new OutputStreamWriter(fOut);
			myOutWriter.append(txtData.getText());
			myOutWriter.close();
			fOut.close();
			Toast.makeText(getBaseContext(),
					"Done writing SD 'mysdfile.txt'",
					Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			Toast.makeText(getBaseContext(), e.getMessage(),
					Toast.LENGTH_SHORT).show();
		}
	}// onClick
	}); // btnWriteSDFile

		btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
		btnReadSDFile.setOnClickListener(new OnClickListener() {

		public void onClick(View v) {
			// write on SD card file data in the text box
		try {
			File myFile = new File("/sdcard/mysdfile.txt");
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(
					new InputStreamReader(fIn));
			String aDataRow = "";
			String aBuffer = "";
			while ((aDataRow = myReader.readLine()) != null) {
				aBuffer += aDataRow + "\n";
			}
			txtData.setText(aBuffer);
			myReader.close();
			Toast.makeText(getBaseContext(),
					"Done reading SD 'mysdfile.txt'",
					Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			Toast.makeText(getBaseContext(), e.getMessage(),
					Toast.LENGTH_SHORT).show();
		}
		}// onClick
		}); // btnReadSDFile

		btnClearScreen = (Button) findViewById(R.id.btnClearScreen);
		btnClearScreen.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// clear text box
				txtData.setText("");
			}
		}); // btnClearScreen

		btnClose = (Button) findViewById(R.id.btnClose);
		btnClose.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// clear text box
				finish();
			}
		}); // btnClose

	}// onCreate

}// AndSDcard


The output of this program will be as shown in the android emulator below.

The main.xml file in your res/layout folder is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000ff"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/txtData"
android:layout_width="fill_parent"
android:layout_height="180px"
android:textSize="18sp"  />

<Button
android:id="@+id/btnWriteSDFile"
android:layout_width="143px"
android:layout_height="44px"
android:text="1. Write SD File"  />

<Button
android:id="@+id/btnClearScreen"
android:layout_width="141px"
android:layout_height="42px"
android:text="2. Clear Screen"  />

<Button
android:id="@+id/btnReadSDFile"
android:layout_width="140px"
android:layout_height="42px"
android:text="3. Read SD File"  />

<Button
android:id="@+id/btnClose"
android:layout_width="141px"
android:layout_height="43px"
android:text="4. Close" />

</LinearLayout>





Most Viewed Articles (in Android )

Latest Articles (in Android)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. thanks! great tutorial!
View Tutorial          By: Gracia at 2013-01-10 07:58:06

2. dude its forces closes everytime what to do .....h
View Tutorial          By: raam at 2013-01-10 13:05:57

3. I get the exception with the message "Permiss
View Tutorial          By: Jan at 2011-07-20 08:58:55

4. In your AndroidManifest.xml file you will have to
View Tutorial          By: Ashley at 2011-07-21 02:47:49

5. I made this "In your AndroidManifest.xml file
View Tutorial          By: lalo at 2011-11-20 05:56:43

6. I am getting error in setContentView(R.layout.main
View Tutorial          By: user at 2012-01-13 06:49:42

7. good one
View Tutorial          By: saurabh at 2012-01-17 04:43:29

8. </application>
<uses-permission an

View Tutorial          By: saurabh at 2012-01-17 05:00:21

9. nice its working good example.... very useful...
View Tutorial          By: Mamatha at 2012-02-02 09:23:40

10. Great little app! Thanks for the help.
View Tutorial          By: Brian at 2012-02-04 04:58:04

11. i am getting a force close...can anyone please hel
View Tutorial          By: jack at 2012-03-22 15:16:40

12. i am getting a force close...can anyone please hel
View Tutorial          By: jack at 2012-03-22 15:18:37

13. <uses-permission android:name="android.per
View Tutorial          By: Dev at 2012-04-06 06:19:03

14. Nice tutorial..for more android tutorial you can c
View Tutorial          By: Tofeeq at 2012-04-21 15:48:08

15. Hi am saving data in to the internal memory.But ho
View Tutorial          By: harsha at 2012-04-27 07:48:45

16. is this code able to read file from(write into) th
View Tutorial          By: lara at 2012-05-14 19:23:36

17. is this code able to read file from(write into) th
View Tutorial          By: lara at 2012-05-14 19:26:30

18. Hi, still having problem with 'Permission denied'.
View Tutorial          By: Dorjoo at 2012-05-18 19:00:22

19. Good Tutorial.
Thanks.

View Tutorial          By: Oki Wicaksono at 2012-05-29 04:09:05

20. Very goog tutorial.
Thanks !!!!

View Tutorial          By: Roberto at 2012-06-01 15:31:07

21. what if i want to create a digital signature on th
View Tutorial          By: Chandra at 2012-06-10 00:52:15

22. Thank You , It's Very Good.
View Tutorial          By: Jack at 2012-06-10 14:09:24

23. gud tutorial but the permission denied is because
View Tutorial          By: amithooda at 2012-06-11 06:43:00

24. Wow, damn nice. It does everything, and the script
View Tutorial          By: Ethan Windsor at 2012-06-14 13:47:39

25. When I use this code it puts everything I append o
View Tutorial          By: Amber at 2012-06-18 15:33:22

26. permission denied I tried giving permission but di
View Tutorial          By: Rudresha at 2012-07-13 08:59:51

27. How do u edit the text ? this is always replacing
View Tutorial          By: anurag at 2012-08-23 07:24:47

28. Its working ,but it overwrite the old one when run
View Tutorial          By: venkat at 2012-09-17 09:38:53

29. I have no mounted the sd card, and I have the &quo
View Tutorial          By: Cgamboa at 2012-12-01 06:39:14

30. my program is run and it created file in the sd ca
View Tutorial          By: mahesh at 2013-01-21 04:15:21

31. i want to open pdf file from sd card and i dont wa
View Tutorial          By: Karan Mavadhiya at 2013-01-30 12:02:30

32. Great tutorial. Working absolutely fine. I needed
View Tutorial          By: Ashwin at 2013-02-28 21:45:29

33. Hi i changed some codes in that and i gave interne
View Tutorial          By: Göktuğ ÖZEL at 2013-03-13 22:44:55

34. Good one......
Thanks.

View Tutorial          By: Suresh at 2013-03-14 07:38:45

35. superb its working perfect but one thing, we have
View Tutorial          By: venkatachalam at 2013-03-14 16:35:00

36. Thank you, very useful example!
View Tutorial          By: Hui Chun Wai at 2013-03-24 15:07:53

37. How to delete file button
View Tutorial          By: Chinzoo at 2013-04-13 13:30:18

38. Thank you, saved my life
View Tutorial          By: Zs at 2013-04-13 22:51:54

39. i added respective code to the Manifest in every c
View Tutorial          By: niko at 2013-04-19 18:23:41

40. also MEGA IMPORTANT:

make sure you

View Tutorial          By: niko at 2013-04-19 18:55:14

41. Thanks.It helps a lot to me.
View Tutorial          By: Sachin at 2013-05-24 19:36:36

42. Hi All........
People those who are ge

View Tutorial          By: Abhijit at 2013-05-28 11:24:24

43. Thanks For This Explanatory Tutorial .
View Tutorial          By: Pralay at 2013-06-15 03:50:26

44. I am getting error in setContentView(R.layout.main
View Tutorial          By: Malick at 2013-06-30 06:57:30

45. Hi,,i try this tutorial,,it working,,but it kind o
View Tutorial          By: Eka Praditya at 2013-07-02 04:09:49

46. Hi,,i try this tutorial,,it working,,but it kind o
View Tutorial          By: Eka Praditya at 2013-07-02 04:11:41

47. thnx for the nice tutorial. but i need to create a
View Tutorial          By: sharath at 2013-09-07 16:29:19

48. Thanks a lot for this nice tutorial, it is very he
View Tutorial          By: Ted at 2014-02-07 12:50:24

49. hi its working nice,can u plz help me how to fetch
View Tutorial          By: Manju gowda at 2014-02-21 07:23:44

50. How can i read PDF files from external memory(SD c
View Tutorial          By: Aswanth at 2014-03-31 09:46:26

51. im trying to just store the data taken from editex
View Tutorial          By: praveen at 2014-08-05 15:19:51

52. I'm Kaveh from Iran - Araz Co.
Thank you fo

View Tutorial          By: Kaveh at 2014-10-02 05:10:12

53. thanx mitra...it helped alot.
View Tutorial          By: psuyog at 2014-10-10 12:16:26

54. where is mysdfile.txt file created. where can i fi
View Tutorial          By: Venkatesh at 2014-11-12 10:53:52

55. Hi, I cant open the file after I click Write SD. I
View Tutorial          By: hassan at 2014-12-17 07:44:16

56. Working !

Nice example, thanks !

View Tutorial          By: Akh at 2015-01-14 14:22:24

57. I need one help how to update particular column da
View Tutorial          By: santhosh.s at 2015-01-22 15:09:33

58. How could i read a file from my ftp server . sugge
View Tutorial          By: Dinshaw at 2015-02-06 07:22:03

59. Thanks guys,
its working perfectly.

View Tutorial          By: Ajmal at 2015-03-25 08:30:07

60. Not able to store on SD card.But storing on phone
View Tutorial          By: kusuma at 2015-07-25 13:42:17

61. this text is overlapping the existing text what if
View Tutorial          By: Alda at 2015-10-02 18:18:53

62. this text is overlapping the existing text what if
View Tutorial          By: Alda at 2015-10-02 18:22:22

63. I have altered the code as described (xml addition
View Tutorial          By: Spijker at 2015-10-14 12:27:47

64. its working!!!!!!!1111 thank u ;-)
View Tutorial          By: Nidhin at 2015-12-04 09:33:54

65. which place store the text file in windows os
View Tutorial          By: balasubramani at 2015-12-31 09:24:50

66. hi
View Tutorial          By: Boopathi.g at 2016-03-31 08:19:58

67. How to make multiple files and2> how to append
View Tutorial          By: Sourav Kumar Nanda at 2016-04-01 07:38:55

68. Thanks guys,
its working perfectly.

View Tutorial          By: Birendra kr verma at 2016-05-04 04:44:01

69. Practical suggestions ! my business partners a few
View Tutorial          By: Pat Redman at 2016-09-15 01:01:43

70. L800 java sd card
View Tutorial          By: Rohitbhbydiya at 2016-10-15 13:02:08

71. It works great! But I have a question, when I have
View Tutorial          By: Harry at 2017-01-07 15:48:56

72. RichardCemo
View Tutorial          By: RichardCemo at 2017-01-30 14:20:00

73. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-02-22 08:07:41

74. RichardCemo
View Tutorial          By: RichardCemo at 2017-02-23 16:26:13

75. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-02-24 11:01:43

76. RichardCemo
View Tutorial          By: RichardCemo at 2017-02-25 05:31:14

77. Thomasmi
View Tutorial          By: Thomasmi at 2017-02-25 15:27:42

78. Thomasmi
View Tutorial          By: Thomasmi at 2017-02-26 03:31:40

79. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-02-28 17:00:48

80. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-02 14:14:22

81. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-05 06:42:49

82. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-06 18:44:20

83. RichardCemo
View Tutorial          By: RichardCemo at 2017-03-12 20:36:06

84. Thomasmi
View Tutorial          By: Thomasmi at 2017-03-13 04:13:57

85. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-13 12:35:19

86. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-14 15:24:39

87. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-14 17:24:18

88. RichardCemo
View Tutorial          By: RichardCemo at 2017-03-15 00:26:14

89. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-20 15:20:52

90. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-29 15:57:21

91. RichardCemo
View Tutorial          By: RichardCemo at 2017-03-30 04:51:21

92. CurtisQuef
View Tutorial          By: CurtisQuef at 2017-03-30 14:04:41

93. Thomasmi
View Tutorial          By: Thomasmi at 2017-04-07 13:35:08

94. Thomasmi
View Tutorial          By: Thomasmi at 2017-04-10 07:26:21

95. RichardCemo
View Tutorial          By: RichardCemo at 2017-04-11 13:27:17

96. I am sure this article has touched all the interne
View Tutorial          By: ______ ______ at 2017-04-17 06:28:01

97. Thomasmi
View Tutorial          By: Thomasmi at 2017-04-18 02:26:39

98. MerlinMub
View Tutorial          By: MerlinMub at 2017-04-21 16:40:03

99. JasonNix
View Tutorial          By: JasonNix at 2017-04-24 19:45:35

100. I know this if off topic but I'm looking into star
View Tutorial          By: Cheap taxi at 2017-05-24 18:24:08

101. Michaelruink
View Tutorial          By: Michaelruink at 2017-06-16 15:36:21

102. XRumerTest
View Tutorial          By: XRumerTest at 2017-07-21 02:07:51

103. What's Happening i am new to this, I stumbled upon
View Tutorial          By: Mobile Game Cheats Tool Android at 2017-07-31 23:48:29

104. MerlinMub
View Tutorial          By: MerlinMub at 2017-08-02 15:27:35

105. MerlinMub
View Tutorial          By: MerlinMub at 2017-08-12 03:38:06