Android------openfileinput, openfileoutput get data from your phone's memory

Source: Internet
Author: User

Openfileoutput and Openfileinput get the files in the phone's memory instead of the SD card.


The context provides two ways to open the file I/O stream in the Data folder of this application.

Openfileinput (String name): the input stream corresponding to the name file

Openfileoutput (String name,int mode): the output stream corresponding to the name file


The second parameter in the output stream represents the mode in which the file is opened, or it can be called a permission:


Mode_private: The file can only be read and written by the current program

Mode_append: Open the file as an append, you can append the content

Mode_world_readable: The contents of this file can be read by other programs

Mode_world_writeable: The contents of this file can be read and written by other programs.



In addition to this, the context provides several ways to access the application's Data folder:

Getdir (String name,int mode): Gets or creates a subdirectory of name in the application's Data folder

File Getfilesdir (): Gets the absolute path of the application's Data folder

String[] FileList (): Returns all files under the Data folder of the application

DeleteFile (String): Deletes the specified file under the Data folder of the application





public class Mainactivity extends Activity {private EditText et;private Button Savebutton, Readbutton;private TextView Sho W;private Boolean mywrite () {try {FileOutputStream fos = openfileoutput ("Data", context.mode_world_readable); String content = Et.gettext (). toString (); Fos.write (Content.getbytes ()); Fos.flush (); Fos.close (); Toast.maketext (Mainactivity.this, "Success", 1). Show (); return true;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return false;} Private Boolean Myread () {try {FileInputStream FIS = openfileinput ("Data"); byte buff[] = new byte[1024]; StringBuffer sb = new StringBuffer (), int hasread = 0;while ((hasread = Fis.read (buff))! =-1) {Sb.append (new String (buff)) ;} Fis.close (); Show.settext (sb.tostring ()); Toast.maketext (Mainactivity.this, Sb.tostring (), 1). Show (); return true;} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return false;} protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); setContentview (r.layout.activity_main); et = (EditText) Findviewbyid (r.id.edittext); Savebutton = (Button) Findviewbyid ( R.id.save) Readbutton = (Button) Findviewbyid (r.id.red); show = (TextView) Findviewbyid (r.id.show); Savebutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubmywrite ();}}); Readbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubmyread ();}});}}


Android------openfileinput, openfileoutput get data from your phone's memory

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.