To develop a Android Application that writes data to the SD Card.
Students can perform IO operation and can manipulate data within SDcard.
Student must familiar with the IO streams in java.io package and to work with.
Creating a New project:
Designing layout for the Android Application:
Code for Activity_main.xml:
android:id=”@+id/editText” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:singleLine=”true” android:textSize=”30dp” />
android:id=”@+id/button” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”10dp” android:text=”Write Data” android:textSize=”30dp” />
android:id=”@+id/button2″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”10dp” android:text=”Read data” android:textSize=”30dp” />
android:id=”@+id/button3″ android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_margin=”10dp” android:text=”Clear” android:textSize=”30dp” />
Adding permissions in Manifest for the Android Application:
Code for AndroidManifest.xml:
android:allowBackup=”true” android:icon=”@mipmap/ic_launcher” android:label=”@string/app_name” android:supportsRtl=”true” android:theme=”@style/AppTheme” >
Java Coding for the Android Application:
public class MainActivity extends AppCompatActivity
Button write,read,clear; @Override
protected void onCreate(Bundle savedInstanceState)
e1= (EditText) findViewById(R.id.editText); write= (Button) findViewById(R.id.button); read= (Button) findViewById(R.id.button2); clear= (Button) findViewById(R.id.button3);
public void onClick(View v)
String message=e1.getText().toString(); try
File f=new File(“/sdcard/myfile.txt”);
FileOutputStream fout=new FileOutputStream(f); fout.write(message.getBytes());
fout.close(); Toast.makeText(getBaseContext(),”Data Written in
catch (Exception e)
public void onClick(View v)
String message; String buf = “”; try
File f = new File(“/sdcard/myfile.txt”); FileInputStream fin = new FileInputStream(f); BufferedReader br = new BufferedReader(new
while ((message = br.readLine()) != null)
fin.close(); Toast.makeText(getBaseContext(),”Data Recived from
catch (Exception e)
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
public void onClick(View v)