How to Create and Write Data into the File in Android

In this tutorial how to create and write data into the file in an Android Application is shown.

We can read and write data in an internal memory as well as external memory of our mobile.

When we read or write data in an external memory then we have to set permission in AndroidManifest.xml file, but when you read or write the data in internal memory then no permissions are required.

The permission to set for external storage to write the data in external file is as below:

<manifest>

<uses-permission android:name=”android:permission.WRITE_EXTERNAL_STORAGE”/>

</manifest>

Write the above code after the <uses-sdk> tag and before the <application> tag.

To acquire a proper directory we can call below methods

  1. getFilesDir()
  2. detCacheDir()

To create new File, create the object of File class like below:

File file=new File(context.getFilesDir(),filename);

We can call openFileOutput() method to get FileOutputStream.

When we open our file in a MODE_PRIVATE then it will overwrite the data, if file exists, if it does not exist then it will create a new file.

When we open our file in a MODE_APPEND then it will append the new data from the last line or end of the file, if file exists, if it does not exist then it will create a new file.

Point to Remember:

  • If you want to see where your Application’s File are stored follow the below mentioned steps:
    1. Click the DDMS button on the top
    2. Open the File Explorer
    3. Open Data folder
    4. Again open the Data folder
    5. Open your package name folder, for ex. com.example.demo
    6. Open ‘files’ folder
    7. Your File data are stored in the file present inside the  ‘files’ folder

This is the code for file internal storage.

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”69dp”
android:text=”File I/O Operations” />

<Button
android:id=”@+id/create1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textView1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”36dp”
android:onClick=”onFileCreate”
android:text=”Create File” />

 

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onFileCreate(View v)
{
Intent in=new Intent(MainActivity.this,create.class);
startActivity(in);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

 

res/layout/create_layout.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<EditText
android:id=”@+id/fnm1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”85dp”
android:ems=”10″
android:hint=”Enter File Name” >

<requestFocus />
</EditText>

<EditText
android:id=”@+id/fdt1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/fnm1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”41dp”
android:ems=”10″
android:hint=”Enter the data” />

<Button
android:id=”@+id/create2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/fdt1″
android:layout_below=”@+id/fdt1″
android:layout_marginTop=”78dp”
android:onClick=”createf”
android:text=”Create File” />

<Button
android:id=”@+id/back1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBottom=”@+id/create2″
android:layout_alignRight=”@+id/fdt1″
android:onClick=”back”
android:text=”Back” />

</RelativeLayout>

src/com.package.demo/create.java

package com.example.demo;

import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class create extends Activity{
FileOutputStream fOut;
EditText fnm,fdt;
String data,fname;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_layout);
fnm=(EditText)findViewById(R.id.fnm1);
fdt=(EditText)findViewById(R.id.fdt1);
}

public void createf(View v) throws IOException
{
fname=fnm.getText().toString();
data=fdt.getText().toString();
try
{
fOut=openFileOutput(fname,MODE_APPEND);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(), “File is successfully created and data is successfully saved in file”, Toast.LENGTH_SHORT).show();
}
catch(Exception e){
e.printStackTrace();
}
finally
{
fOut.close();
}
}

public void back(View v)
{
Intent in=new Intent(this,MainActivity.class);
startActivity(in);
}

}

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.demo.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.create”></activity>

</application>

</manifest>

https://youtu.be/MCqwnfFFHRk

Add a Comment

Your email address will not be published. Required fields are marked *