Android Development Basics Tutorial-file storage function Implementation _android

Source: Internet
Author: User
Tags gettext
File storage:
Copy Code code as follows:

public class Mainactivity extends activity {
EditText Mname, Mage;
TextView MTV;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mname = (edittext) Findviewbyid (R.ID.EDITTEXT1);
Mage = (edittext) Findviewbyid (R.ID.EDITTEXT2);
MTV = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
}
public void OnClick (View v) {
String name = Mname.gettext (). toString ();
int age = Integer.parseint (Mage.gettext (). toString ());
String cont = "Name=" + name + ", age=" + age + "\ n";
try {
int id = V.getid ();
Internal Save
if (id = = R.id.button1) {
FileOutputStream fos = this.openfileoutput ("Mytext.txt",
Context.mode_append | Context.mode_world_writeable
| context.mode_world_readable);
Fos.write (Cont.getbytes ());
Fos.close ();
Toast.maketext (This, write complete, 1). Show ();
}
Read
else if (id = = R.id.button2) {
FileInputStream FIS = this.openfileinput ("Mytext.txt");
byte[] bytes = new byte[fis.available ()];
Fis.read (bytes);
Fis.close ();
String str = new string (bytes);
Mtv.settext (str);
}
catch (Exception e) {
E.printstacktrace ();
}
}

Other apps if you want to access this mytext.txt file format as follows:
Copy Code code as follows:

public class Mainactivity extends activity {
TextView mcontent;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Mcontent= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
}
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.button1:
try {
Readremotefilebyabslutepath ();
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Break
Case R.id.button2:
try {
Writeremotefilebyabslutepath ();
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Break
Default
Break
}
}
/**
* Read remote files through file absolute path
* @throws Exception
*/
public void Readremotefilebyabslutepath () throws exception{
String Path = "/data/data/com.nanguabing.filedemo/files/mytext.txt";
FileInputStream fis = new FileInputStream (path);
byte[] bytes = new byte[fis.available ()];
Fis.read (bytes);
Fis.close ();
String str = new string (bytes);
Mcontent.settext (str);
LOG.I ("other", str);
}
/**
* Read remote files through file absolute path
* @throws Exception
*/
public void Writeremotefilebyabslutepath () throws exception{
String Path = "/data/data/com.nanguabing.filedemo/files/mytext.txt";
FileOutputStream fos = new FileOutputStream (path,true);
Fos.write ("Other write!"). GetBytes ());
Fos.close ();
LOG.I ("Other", "other write over!");
}
/**
* Write remote files via package related context
* @throws Exception
*/
public void Readromotebypackagecontext () throws Exception {
String pname = "Com.nanguabing.filedemo";
Context ctx = This.createpackagecontext (PName,
context.context_ignore_security);
FileInputStream FIS = ctx.openfileinput ("Mytext.txt");
byte[] bytes = new byte[fis.available ()];
Fis.read (bytes);
Fis.close ();
LOG.I ("Other", New String (bytes));
}
/**
* Write remote files via package related context
*/
public void ReadRomoteByPackageContext2 () throws Exception {
String pname = "Com.nanguabing.filedemo";
Context ctx = This.createpackagecontext (PName,
Context.context_include_code);
FileInputStream FIS = ctx.openfileinput ("Mytext.txt");
byte[] bytes = new byte[fis.available ()];
Fis.read (bytes);
Fis.close ();
LOG.I ("Other", New String (bytes));
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}
Related Article

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.