- package com.example.app8;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import android.os.Environment;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- Button save,load;
- EditText message;
- TextView t1;
- String Message1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- save=(Button)findViewById(R.id.button1);
- load=(Button)findViewById(R.id.button2);
- message=(EditText)findViewById(R.id.editText1);
- t1=(TextView)findViewById(R.id.textView1);
- save.setOnClickListener(new View.OnClickListener(){
- public void onClick(View v){
- //Get message from user store in message1 variable
- Message1 =message.getText().toString();
- try{
- //Create a new folder called MyDirectory in SDCard
- File sdcard=Environment.getExternalStorageDirectory();
- File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
- directory.mkdirs();
- //Create a new file name textfile.txt inside MyDirectory
- File file=new File(directory,"textfile.txt");
- FileOutputStream fou=new FileOutputStream(file);
- OutputStreamWriter osw=new OutputStreamWriter(fou);
- try{
- //write a user data to file
- osw.append(Message1);
- osw.flush();
- osw.close();
- Toast.makeText(getBaseContext(),"Datasaved",Toast.LENGTH_LONG).show();
- }catch(IOException e){
- e.printStackTrace();
- }
- }catch (FileNotFoundException e){
- e.printStackTrace();
- }
- }
- });
- load.setOnClickListener(new View.OnClickListener(){
- public void onClick(View v){
- try{
- File sdcard=Environment.getExternalStorageDirectory();
- File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
- File file=new File(directory,"textfile.txt");
- FileInputStream fis=new FileInputStream(file);
- InputStreamReader isr=new InputStreamReader(fis);
- char[] data=new char[100];
- String final_data="";
- int size;
- try{
- while((size=isr.read(data))>0)
- {
- //read a data from file
- String read_data=String.copyValueOf(data,0,size);
- final_data+=read_data;
- data=new char[100];
- }
- //display the data in output
- Toast.makeText(getBaseContext(),"Message:"+final_data,Toast.LENGTH_LONG).show()
- ;
- }catch(IOException e){
- e.printStackTrace();
- }
- }catch (FileNotFoundException e){
- e.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;
- }
- }
- <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: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" >
- <EditText
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="32dp"
- android:ems="10" android:inputType="text">
- <requestFocus />
- </EditText>
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/editText1"
- android:layout_marginTop="41dp"
- android:text="SAVEDATA"/>
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/button1"
- android:layout_alignBottom="@+id/button1"
- android:layout_alignParentRight="true"
- android:text="SHOWDATA"/>
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@+id/linearLayout1"
- android:layout_centerHorizontal="true"/>
- </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.app8"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <uses-permission
- android:name="android.permission.WRITE_EXTERNAL_STORAGE">
- </uses-permission>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.app8.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>