Internal Storage in Android

How to Save a File in Internal Storage in Android

CLASS FILE

package com.example.malik.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {

EditText editText;
TextView textView;

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

editText = (EditText) findViewById(R.id.etData);
textView = (TextView) findViewById(R.id.tvData);

}

public void read(View view){
try {
FileInputStream fileInputStream = openFileInput(“myText.txt”);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String lines;
while ((lines = bufferedReader.readLine()) != null){
stringBuffer.append(lines + “\n”);
}
textView.setText(stringBuffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void write(View view){
String myTextMessage = editText.getText().toString();
try {
FileOutputStream fileOutputStream = openFileOutput(“myText.txt”, MODE_PRIVATE);
fileOutputStream.write(myTextMessage.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), “Text Saved”, Toast.LENGTH_SHORT).show();
editText.setText(“”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

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.malik.test.MainActivity”>
<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Read”
android:id=”@+id/button”
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true”
android:onClick=”read”
/>

<Button
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Write”
android:id=”@+id/button2″
android:layout_below=”@+id/button”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignRight=”@+id/etData”
android:layout_alignEnd=”@+id/etData”
android:onClick=”write”
/>

<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/etData”
android:layout_below=”@+id/button2″
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true”
android:lines=”5″ />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceMedium”
android:text=”Result here”
android:id=”@+id/tvData”
android:layout_below=”@+id/etData”
android:layout_alignParentLeft=”true”
android:layout_alignParentStart=”true”
android:layout_marginTop=”45dp”
android:layout_alignRight=”@+id/etData”
android:layout_alignEnd=”@+id/etData” />
</RelativeLayout>

Output :

pic

 

 

Share

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.