Learn file handling in android apps.
MainActivity.java
package com.example.ashish.filehandleing;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void open(View v )
{
AlertDialog.Builder alertdialogbuilder=new AlertDialog.Builder(this);
alertdialogbuilder.setMessage("Are you sure to save the file");
alertdialogbuilder.setPositiveButton("yes",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface arg ,int gk)
{
EditText ed = (EditText) findViewById(R.id.editText);
String tv = ed.getText().toString();
try {
FileOutputStream fileos = openFileOutput("short.txt", MODE_PRIVATE);
OutputStreamWriter outputwriter = new OutputStreamWriter(fileos);
outputwriter.write(ed.getText().toString());
outputwriter.close();
Toast.makeText(MainActivity.this,"Your file is saved",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
alertdialogbuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dilog, int which) {
finish();
}
});
AlertDialog alertdialog=alertdialogbuilder.create();
alertdialog.show();
}
public void readfile(View v)
{
try
{
TextView text=(TextView)findViewById(R.id.textView2);
int c;
String str="";
FileInputStream FI=openFileInput("short.txt");
while((c=FI.read())!=-1)
{
str=str+(char)c;
}
text.setText(str);
FI.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/abc" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_marginTop="54dp"
android:onClick="open"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read"
android:id="@+id/button2"
android:onClick="readfile"
android:layout_above="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="56dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="85dp" />
</RelativeLayout>
Comments
Post a Comment