@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button save = (Button) findViewById(R.id.save);
Button display = (Button) findViewById(R.id.display);
final EditText et1 = (EditText) findViewById(R.id.txtname);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(et1.getText().toString());
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File is saved successfully!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
display.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
FileInputStream fileIn=openFileInput("mytextfile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[200];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
// char to string conversion
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
if(s.equals("")) {
Toast.makeText(getBaseContext(), "Content is not available in the file",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Contents in the file\n" + s,
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent" android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Username"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:textColor="@android:color/holo_blue_light" />
<EditText
android:id="@+id/txtname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="20"
android:hint="Enter Name"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:inputType="textPersonName"
/>
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="10dp"
android:background="@color/colorAccent"
android:text="SAVE"
android:textColor="@android:color/white"
android:textSize="25sp" />
<Button
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="70dp"
android:background="@color/colorAccent"
android:text="DISPLAY"
android:textColor="@android:color/white"
android:textSize="25sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Developed By : w3techblog.com"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:textColor="@android:color/holo_blue_light" />
</LinearLayout>
</ScrollView>