package gr.mybook.myfilesave;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	Button btn1, btn2;
	EditText text;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btn1 = (Button)findViewById(R.id.button1);
		btn2 = (Button)findViewById(R.id.button2);
		text =(EditText)findViewById(R.id.editText1);
	}
	
	public void Save(View v) {
		String str = text.getText().toString();
		try {
			FileOutputStream fOut = openFileOutput("temp.txt", MODE_PRIVATE);
			OutputStreamWriter outstr = new OutputStreamWriter(fOut);
			outstr.write(str);
			outstr.flush();
			outstr.close();
			Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
			text.setText("");
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public void Load(View v) {
		try {
		FileInputStream fIn = openFileInput("temp.txt");
		InputStreamReader instr = new InputStreamReader(fIn);
		BufferedReader bread = new BufferedReader(instr);
		String str;
		StringBuilder buffer = new StringBuilder();
		while ((str = bread.readLine()) != null) {
			buffer.append(str);
		}
		fIn.close();
		text.setText(buffer.toString());
		Toast.makeText(this, "Data Loaded", Toast.LENGTH_SHORT).show();
		}
		catch (IOException ioe) {
		ioe.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
