DAVLEEN KAUR MATTA
BE-4/B
Niraj Bamboli
ROLL NO-33
BE-4/A
Roll No.: 1
EXPERIMENT-4
Experiment No. 3
Develop an application that writes data to the SD card.
Source Code:
//manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.datasdcard">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
//xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="35dp">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ip"
android:hint="Enter the text you want to add to file"
android:textSize="24sp"
android:padding="4dp"/>
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="50dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Niraj Bamboli
BE-4/A
Roll No.: 1
android:id="@+id/write"
android:text="Write To SD card"
android:layout_marginHorizontal="20dp"
android:onClick="write"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/read"
android:text="Read From SD card"
android:layout_marginHorizontal="20dp"
android:onClick="read"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:textSize="30sp"
android:layout_marginTop="50dp"/>
</LinearLayout>
//java
package com.example.datasdcard;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextInputEditText ip = findViewById(R.id.ip);
final TextView tv = findViewById(R.id.text);
Button wrbtn = findViewById(R.id.write);
Button rdbtn = findViewById(R.id.read);
final String FILE_NAME="file.txt";
wrbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
File f = new File(getFilesDir() + FILE_NAME);
f.createNewFile();
FileWriter fw=new FileWriter(f);
String data = ip.getText().toString();
fw.write(data);
fw.flush();
fw.close();
tv.setText("Data Write Successful!!!"); }
catch (IOException e){
e.printStackTrace();}}});
rdbtn.setOnClickListener(new View.OnClickListener() {
Niraj Bamboli
BE-4/A
Roll No.: 1
@Override
public void onClick(View view) {
try {
File f = new File(getFilesDir() + FILE_NAME);
f.createNewFile();
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String data="",t;
while((t=br.readLine())!=null){
data+=t;}
tv.setText(data);
br.close();}
catch (IOException e){
e.printStackTrace();
}}});}}
Output: