How to store data in Android: File form

Source: Internet
Author: User
Tags save file throwable

Summarized in Android, there are 5 ways to store data. Make a note-taking arrangement today. about how to save data in the form of a file.

1. Design the layout in Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "android:orient        ation= "vertical" > <textview android:id= "@+id/textname" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "file name"/> <edittext android:id= "@+id/inputName         "Android:layout_width=" match_parent "android:layout_height=" wrap_content "/> <textview         Android:id= "@+id/filecontextname" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "File contents"/> <edittext android:id= "@+id/inputcontext" Android:layout_width= "Ma Tch_parent "android:layout_height=" Wrap_content "android:maxlines=" 5 "/> <button Androi D:id= "@+id/filesavebtn" Android:layout_wIdth= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Save"/> </LinearLayout> 


2. In Mainactivity.class get the user input file name and file information, save the file.

public class Mainactivity extends Activity {private Button savebtn;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        SAVEBTN = (Button) Findviewbyid (R.ID.FILESAVEBTN);               Savebtn.setonclicklistener (New Savefilelistener ());}            }

/** * If this class does not need to be     Inherit, add a final keyword to end it. */Private Final class Savefilelistener implements onclicklistener{@Overridepublic void OnClick (View v) {EditText filen        Ametext = (EditText) Findviewbyid (r.id.inputname);        EditText Filecontenttext = (EditText) Findviewbyid (R.id.inputcontext); String fileName = Filenametext.gettext (). toString (); String filecontent = Filecontenttext.gettext (). toString (); Fileservice fileservice = new Fileservice (Getapplicationcontext ()); try {//Save to Phone memory fileservice.savefileprivate ( filename,filecontent); <span style= "White-space:pre" ></SPAN>} catch (Throwable e) {<span style= " White-space:pre "></span>toast.maketext (Mainactivity.this, R.string.fail, Toast.LENGTH_LONG). Show ();< Span style= "White-space:pre" ></span>e.printstacktrace ();} 
     }}

3. At the business level, Fileserivice handles how the file contents are saved.

public class Fileservice {Private context context;//generate constructor Public Fileservice (context context) {This.context = context;} /*** Save File Information * @param filename * @param filecontent file contents * @throws throwable*/public void savefileprivate (String filename, String filecontent) throws exception{//uses the output stream object to output the data we need. Indicate the location required to export Openfileoutput ("Accept only file name, do not accept file path",//"To specify the operation mode of the file, behavior" append mode "," Overwrite form "//file has one access control, target owner, access rights for other users// Private mode of operation: Files created can only be accessed by the app, and other apps cannot access the file. Second, private mode creates files that overwrite the original file contents. The default is guaranteed to exist under the Files folder under which the current app package resides. FileOutputStream outputstream = context.openfileoutput ("Lmfcast.txt", context.mode_private);//Append method// FileOutputStream outputstream = context.openfileoutput ("Lmfcast.txt", context.mode_append); write (Filecontent, OutputStream);} private void Write (String filecontent, FileOutputStream outputstream) throws IOException {Outputstream.write ( Filecontent.getbytes ()); Outputstream.close ();}} 


4. Source code operation:


Learning Summary:

1. File saving method, is the use of Java IO Stream technology.

2. The file access mode is divided into private and append.

3. File operation behavior is divided into: private,apend,read,write,read+write.





How to store data in Android: File form

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.