Hi,Today we are going to learn about save some data to file internally in android.
Storage Options in android:
1) Sqlite Database
2) Sharedpreferences
3) Internal File Storage
4) External File Storage(i.e., In SDCARD)
File Internal Storage means any kind of data we can store to file internally i.e., in internal memory of device
we can store any kind of data to any kind of file.It is very easy and simple
Create a Project:
layout file
Create a UI file in res/layout folder
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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="save" />
</RelativeLayout>
MainActivity.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText getedit;
Button savebtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getedit=(EditText)findViewById(R.id.editText1);
savebtn=(Button)findViewById(R.id.button1);
savebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String getdata=getedit.getText().toString();
String filename = "internalsave.txt";
String filepath = "sample";
File myInternalFile;
// Log.e("log_tag", "Error converting result "+finalresult1);
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
@SuppressWarnings("deprecation")
File directory = contextWrapper.getDir(filepath, Context.MODE_WORLD_READABLE);
myInternalFile = new File(directory , filename);
try {
FileOutputStream fos = new FileOutputStream(myInternalFile);
fos.write(getdata.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</application>
</manifest>
if you want to get data from internal file storage just refer here
Finally,Run your Application
OUTPUT:
enter any data if you want then click on save button.It will be saved internally,then
go to your File Explorer there
we can find a created file at data/data/your package name/sample/internalsave.txt
Storage Options in android:
1) Sqlite Database
2) Sharedpreferences
3) Internal File Storage
4) External File Storage(i.e., In SDCARD)
File Internal Storage means any kind of data we can store to file internally i.e., in internal memory of device
we can store any kind of data to any kind of file.It is very easy and simple
Create a Project:
layout file
Create a UI file in res/layout folder
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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="save" />
</RelativeLayout>
MainActivity.java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText getedit;
Button savebtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getedit=(EditText)findViewById(R.id.editText1);
savebtn=(Button)findViewById(R.id.button1);
savebtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String getdata=getedit.getText().toString();
String filename = "internalsave.txt";
String filepath = "sample";
File myInternalFile;
// Log.e("log_tag", "Error converting result "+finalresult1);
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
@SuppressWarnings("deprecation")
File directory = contextWrapper.getDir(filepath, Context.MODE_WORLD_READABLE);
myInternalFile = new File(directory , filename);
try {
FileOutputStream fos = new FileOutputStream(myInternalFile);
fos.write(getdata.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</application>
</manifest>
Finally,Run your Application
OUTPUT:
enter any data if you want then click on save button.It will be saved internally,then
go to your File Explorer there
we can find a created file at data/data/your package name/sample/internalsave.txt