MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.example.bhaumik.internalstorage.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginTop="10dp"
android:hint="Enter a Message"
android:gravity="top"
android:textSize="18dp"
android:id="@+id/et_message"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Write Message"
android:id="@+id/btn_write"
android:textAllCaps="false" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read Message"
android:textAllCaps="false"
android:id="@+id/btn_read"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:hint="Show Message"
android:textSize="18dp"
android:id="@+id/tv_show"/>
</LinearLayout>
MainActivity.java
package com.example.bhaumik.internalstorage;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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;
Button write,read;
String file_name = "bhaumik";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.et_message);
textView = (TextView) findViewById(R.id.tv_show);
write = (Button) findViewById(R.id.btn_write);
read = (Button) findViewById(R.id.btn_read);
textView.setVisibility(View.GONE);
write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = editText.getText().toString();
String file_name = "bhaumik";
try {
FileOutputStream fileOutputStream = openFileOutput(file_name,MODE_PRIVATE);
fileOutputStream.write(msg.getBytes());
fileOutputStream.close();
Toast.makeText(MainActivity.this,"Message Saved",Toast.LENGTH_SHORT).show();
editText.setText("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String messages;
try {
FileInputStream fileInputStream = openFileInput("bhaumik");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
while ((messages = bufferedReader.readLine()) != null)
{
stringBuffer.append(messages + "\n");
}
textView.setText(stringBuffer.toString());
textView.setVisibility(View.VISIBLE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
0 comments:
Post a Comment