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