LAB 13 Write Data To A File and Read Data From The File
LAB 13 Write Data To A File and Read Data From The File
Roll No:1266
EXPERIMENT 13
WORKING WITH FILES
Aim:
To create an application that will write data to a file and read data from the file.
THEORY:
Android provides many kinds of storage for applications to store their data. These
storage places are shared preferences, internal and external storage, SQLite storage,
and storage via network connection.
In this chapter we are going to look at the internal storage. Internal storage is the
storage of the private data on the device memory.
By default these files are private and are accessed by only your application and get
deleted , when user delete your application.
Writing File
In order to use internal storage to write some data in the file, call the openFileOutput()
method with the name of the file and the mode. Its syntax is given below –
FileOutputStream fOut = openFileOutput("file name
here",MODE_WORLD_READABLE);
Reading File
In order to read from the file you just created , call the openFileInput() method with
the name of the file. It returns an instance of FileInputStream. Its syntax is given
below –
FileInputStream fin = openFileInput(file);
After that, you can call read method to read one character at a time from the file and
then you can print it. Its syntax is given below –
int c;
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
Assignment
1. Write a program to create a file in a directory and perform following file
operation.
a) Write into a file
b) Read from a file
CODE:
activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:id="@+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Files Demo"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
android:layout_centerHorizontal="true"
android:textColor="#6621f0"
android:textStyle="bold"
></TextView>
<EditText
android:id="@+id/txtmsg"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:hint="Enter Message"
android:textSize="20dp"
android:layout_below="@+id/head"
android:layout_margin="15dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnwrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_marginStart="67dp"
android:layout_marginTop="153dp"
android:background="#16d6a6"
android:text="Write"
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="@+id/btnread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/btnwrite"
android:layout_gravity="center"
android:layout_marginEnd="87dp"
android:background="#d68316"
android:text="Read"
android:textColor="#fff"
android:textStyle="bold" />
</RelativeLayout>
MainActivity.java
package com.example.mcamock.filesdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
EditText input;
Button write,read;
String file_naem ="file.txt";
String msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = findViewById(R.id.txtmsg);
write=findViewById(R.id.btnwrite);
read=findViewById(R.id.btnread);
write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
msg = input.getText().toString();
try {
FileOutputStream fos = openFileOutput(file_naem,MODE_PRIVATE);
fos.write(msg.getBytes());
fos.close();
Toast.makeText(getApplicationContext(),"File Saved" +
getFilesDir(),Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileInputStream fileInputStream = openFileInput(file_naem);
int cha;
String temp = null;
while((cha=fileInputStream.read())!=1)
{
temp = temp+Character.toString((char)cha);
}
input.setText(temp);
fileInputStream.close();
Toast.makeText(getApplicationContext(),"File Read" +
getFilesDir(),Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
}
}
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mcamock.filesdemo">
<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" />
</manifest>
files.txt
Hello Welcome to MCA
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
OUTPUT: