Android FileInputStream and FileOutputStream

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.