android--file read/write

Source: Internet
Author: User

Package Com.zx.filedemo2;

Import Java.io.ByteArrayOutputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
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 TextView TextView;
Private Button Btnbutton;
Private EditText EditText;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

textview= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
btnbutton= (Button) Findviewbyid (R.id.button1);
edittext= (EditText) Findviewbyid (R.ID.EDITTEXT1);

Btnbutton.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated method stubs
Writedate (Edittext.gettext (). toString ());
Textview.settext (Readdate ());
}
});
}

public void Writedate (String contentstring) {
try {
FileOutputStream stream = Openfileoutput ("A.txt", mode_private);
Stream.Write (Contentstring.getbytes ());
Stream.Close ();
} catch (FileNotFoundException e) {
TODO Auto-generated catch block
E.printstacktrace ();
} catch (IOException e) {
TODO Auto-generated catch block
E.printstacktrace ();
}
}

Public String readdate () {
String Str=null;
try {
FileInputStream stream = Openfileinput ("A.txt");
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int len = 0;
while (len = stream.read (buffer))!=-1) {
Baos.write (Buffer,0,len);
}
str = baos.tostring ();
Baos.close ();
Stream.Close ();
} catch (FileNotFoundException e) {
TODO Auto-generated catch block
E.printstacktrace ();
} catch (IOException e) {
TODO Auto-generated catch block
E.printstacktrace ();
}

return str;
}

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

@Override
public boolean onoptionsitemselected (MenuItem item) {
Handle Action Bar Item clicks here. The Action Bar would
Automatically handle clicks on the Home/up button, so long
As you specify a the parent activity in Androidmanifest.xml.
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
}

android--file read/write

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.