Package com. example. file;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import org. apache. http. util. EncodingUtils;
Import android. app. Activity;
Import android. content. Context;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Private static final String FILE_NAME = "file_out.txt ";
Button mButton1, mButton2;
EditText mEditText;
TextView mTextView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MButton1 = (Button) findViewById (R. id. button1 );
MButton2 = (Button) findViewById (R. id. button2 );
MEditText = (EditText) findViewById (R. id. editText1 );
MTextView = (TextView) findViewById (R. id. textView1 );
MButton2.setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Try {
FileInputStream fileInputStream = openFileInput (FILE_NAME );
Byte [] buffer = new byte [1024];
FileInputStream. read (buffer );
String filecontentString = EncodingUtils. getString (buffer, "UTF-8 ");
MTextView. setText (filecontentString );
MTextView. setTextColor (Color. YELLOW );
}
Catch (Exception e ){
// TODO: handle exception
}
}
});
MButton1.setOnClickListener (new OnClickListener (){
Public void onClick (View v ){
// TODO Auto-generated method stub
Try {
// Append and save
FileOutputStream fileOutputStream = openFileOutput (FILE_NAME, Context. MODE_APPEND );
String inputfilecontent = mEditText. getText (). toString ();
FileOutputStream. write (inputfilecontent. getBytes ());
FileOutputStream. close ();
}
Catch (Exception e ){
// TODO: handle exception
}
}
});
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}