C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Android

Write file using FileOutputStream class in Android

In this article, we are going to learn about files, how to write content using FileOutputStream and save them in device internal storage?
Submitted by Manu Jemini, on February 08, 2018

In the example below, we are going to use the FileOutputStream class to save the text in a file and Toast class to make a text on the screen that the text has been saved.

To implement this in your program you will need to import in java file:

    java.io.FileOutputStream
    android.widget.Toast

Then create a layout file with a single EditText and Button like this:

<EditText
	android:layout_width="400dp"
	android:layout_height="wrap_content"
	android:id="@+id/editText"
	android:hint="Enter your text here.."
	android:focusable="true"
	android:textColorHint="#ff7eff15"
	/>

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Click to save text"
	android:id="@+id/button"
	android:layout_below="@+id/editText"
	android:onClick="save"/>

In the Button above we have put an onClick property and set it to save function which is in the java File.

First, we will take the reference of the EditText and Button from the layout file, set a string variable with the name of the file.

Then whenever user clicks the button we take the value of the EditText and convert it into the string and save it in the file mentioned. Like this:

    FileOutputStream F = openFileOutput(file,MODE_WORLD_READABLE);
    F.write(d.getBytes());
    F.close();

In the end, make a Toast to tell that it’s been done.

1) Java file:

package com.example.hp.myapplication;

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.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileOutputStream;


public class MainActivity extends AppCompatActivity{
	String d;
	String file = "myappdata";

	EditText E;

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

		E=(EditText)findViewById(R.id.editText);
	}

	public void save(View v){
		d=E.getText().toString();
		try {
			FileOutputStream F = openFileOutput(file,MODE_WORLD_READABLE);
			F.write(d.getBytes());
			F.close();
			Toast.makeText(getBaseContext(),"Your text file saved SUCCESSFULLY",Toast.LENGTH_SHORT).show();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.hp.myapplication.MainActivity">
    <EditText
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="Enter your text here.."
        android:focusable="true"
        android:textColorHint="#ff7eff15"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to save text"
        android:id="@+id/button"
        android:layout_below="@+id/editText"
        android:layout_alignParentStart="true"
        android:onClick="save"/>
</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

Output

Write file using FileOutputStream class in Android 1
Write file using FileOutputStream class in Android 2



Liked this article? Do share with your friends :)


COMMENTS