String str = “TutorialRide”;
fopStream.write(str.getBytes());
fopStream.close();
int read = -1;
StringBuffersb = newStringBuffer();
FileInputStreamfipStream = openFileInput(“file_name”);
while((read = fibStream.read()) != -1)
{
sb.append((char)read);
}
Following internal storage example demonstrates, how the user's data is stored into an Android internal memory.
Write Operation
File name: activity_main.xml
<RelativeLayoutxmlns: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">
<Button
android:id = "@+id/btnNext"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/btnSave"
android:layout_marginLeft = "35dp"
android:layout_toRightOf = "@+id/btnSave"
android:text = "Next Activity"
android:onClick = "next"
/>
<TextView
android:id = "@+id/textView2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@+id/textView1"
android:layout_below = "@+id/txtUserName"
android:layout_marginTop = "70dp"
android:text = "Password"
android:textSize = "15dp"
/>
<EditText
android:id = "@+id/txtPassword"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/textView2"
android:layout_toRightOf = "@+id/textView1"
android:ems = "10"
android:inputType = "textPassword">
</EditText>
<Button
android:id = "@+id/btnSave"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_below = "@+id/textView2"
android:layout_marginTop = "96dp"
android:layout_toLeftOf = "@+id/editText1"
android:text = "Save"
android:onClick = "save"
/>
<TextView
android:id = "@+id/textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@+id/btnSave"
android:layout_alignParentTop = "true"
android:layout_marginTop = "30dp"
android:text = "UserName"
android:textSize = "15dp"/>
<EditText
android:id="@+id/txtUserName"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/textView1"
android:layout_alignLeft = "@+id/txtPassword"
android:ems = "10"/>
<requestFocus/>
</RelativeLayout>
File name: MainActivity.java
public class MainActivity extends Activity
{
EditTextname,password;
FileOutputStreamfopStream;
File file = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//XML file name activity_main.xml
name = (EditText)findViewById(R.id.txtUserName);
password = (EditText)findViewById(R.id.txtPassword);
}
public void save(View v)
{
String txt1 = name.getText().toString();
String txt2 = password.getText().toString();
txt1 = txt1+" ";
try
{
file = getFilesDir();
fopStream = openFileOutput ("data.txt",Context.MODE_PRIVATE);
fopStream.write(txt1.getBytes ());
fopStream.write(txt2.getBytes ());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fopStream.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(), "Data Stored Successfully at location "+file, Toast.LENGTH_LONG).show();
}
public void next(View v)
{
Toast.makeText(getApplicationContext(), "Next Activity called", Toast.LENGTH_LONG).show();
Intent intent = newIntent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
}