Basic android development tutorial-file storage function implementation

Source: Internet
Author: User

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

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.