File Storage:
Copy codeThe Code is 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 ();
// Save internally
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 completed", 1). show ();
}
// Read
Else if (id = R. id. button2 ){
FileInputStream FCM = this. openFileInput ("mytext.txt ");
Byte [] bytes = new byte [FCM. available ()];
FS. read (bytes );
FCM. close ();
String str = new String (bytes );
Mtv. setText (str );
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
If you want to upload this mytext.txt file, the format is as follows:Copy codeThe Code is 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 a remote file through the absolute path of the file
* @ Throws Exception
*/
Public void readRemoteFileByAbslutePath () throws Exception {
String path = "/data/com. nanguabing. filedemo/files/mytext.txt ";
FileInputStream FCM = new FileInputStream (path );
Byte [] bytes = new byte [FCM. available ()];
FS. read (bytes );
FCM. close ();
String str = new String (bytes );
Mcontent. setText (str );
Log. I ("Other", str );
}
/**
* Read a remote file through the absolute path of the file
* @ Throws Exception
*/
Public void WriteRemoteFileByAbslutePath () throws Exception {
String path = "/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 a remote file using the 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 FCM = ctx. openFileInput ("mytext.txt ");
Byte [] bytes = new byte [FCM. available ()];
FS. read (bytes );
FCM. close ();
Log. I ("Other", new String (bytes ));
}
/**
* Write a remote file using the package-related context
*/
Public void readRomoteByPackageContext2 () throws Exception {
String pname = "com. nanguabing. filedemo ";
Context ctx = this. createPackageContext (pname,
Context. CONTEXT_INCLUDE_CODE );
FileInputStream FCM = ctx. openFileInput ("mytext.txt ");
Byte [] bytes = new byte [FCM. available ()];
FS. read (bytes );
FCM. 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;
}
}