Simple Android reads and writes files

Source: Internet
Author: User
Environment: 1. system: window72.android version: 2.23.eclipse3.5.2 (Galileo) I. Basic flowchart (writing files) II. simulator running interface 1. main Interface 2. save (non-conforming) 3. save normally 4. read files 5. editor 3, Code 1. save the main code of the file. Save the main code 1 bsave. setonclicklistener (new view. onclicklistener () {2 3 @ override 4 Public void onclick (view v) {5 // todo auto-generated method stub 6 resid = R. string. success; 7 file_name = filename. gettext (). tostring (); 8 file_content = filecontent. gettext (). tostring (); 9/* openfileoutput api10 * fileoutputstream openfileoutput (string name, int mode) 11 * open a private file associated wi Th this context's application package for writing. creates the file if it doesn' t already exist.12 */13/* context four modes api14 * context. mode_append --> If the file already exists then write data to the end of the existing file instead of erasing it.15 * context. mode_private --> the default mode, where the created file can only be accessed by the calling application (or all applications sharing the SA Me user ID ). 16 * context. mode_world_readable --> allow all other applications to have read access to the created file.17 * context. mode_world_writeable --> allow all other applications to have write access to the created file.18 */19 flag = insertinfo (); 20 if (FLAG) {21 try {22 outputstream outstream = filestoreactivity. this. openfileoutput (file_name, mode_private); 23 fileservice. save (outstream, file_conte NT); 24 25} catch (exception e) {26 log. E (TAG, E. tostring (); 27 resid = R. string. error; 28} 29 toast. maketext (filestoreactivity. this, resid, toast. length_long ). show (); 30} 31} 32 33 34}); 2. read the main code of the file to read the main code 1 button bread = (button) findviewbyid (R. id. read); 2 bread. setonclicklistener (new view. onclicklistener () {3 4 @ override 5 Public void onclick (view v) {6 // todo auto-generated method stub 7 // The file content is hidden at this time The label 8 filecontent of the box and file content. setvisibility (view. gone); 9 textview content = (textview) findviewbyid (R. id. content); 10 content. setvisibility (view. gone); 11 inputstream instream; 12 file_name = filename. gettext (). tostring (); 13 14/* api15 * fileinputstream openfileinput (string name) 16 * open a private file associated with this context's application package for reading. 17 */18 try {19 instream = filestoreac Tietong. this. openfileinput (file_name); 20 string context = fileservice. read (instream ). tostring (); 21 toast. maketext (filestoreactivity. this, context, toast. length_long ). show (); 22} catch (exception e) {23 log. E (TAG, E. tostring (); 24 resid = R. string. error; 25} 26} 27}); 3. about the editor's code about the encoding code button babout = (button) findviewbyid (R. id. about); babout. setonclicklistener (new view. onclicklistener () {@ override public Vo Id onclick (view v) {// todo auto-generated method stub new alertdialog. builder (filestoreactivity. this ). settitle ("editor information "). setmessage ("Editor: naive_monk" + "\ n" + "Hobbies: Basketball, music, dating" + "\ n" + "Q: 1271522052 "+" \ n "+" Email: summerdir@gmail.com "+" \ n "+" City: Huizhou, Guangdong "+" \ n "). setcancelable (false ). setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface Dia Log, int ID) {// todo auto-generated method stub dialog. dismiss ();}}). setnegativebutton ("exit", new dialoginterface. onclicklistener () {@ override public void onclick (dialoginterface dialog, int ID) {// todo auto-generated method stub dialog. dismiss ();}}). show () ;}}); 4. business Code code/** save file */public static void save (outputstream outstream, string content) throws exception {// write data outstream. WRI Te (content. getbytes (); outstream. close ();}/** Read File */public static string read (inputstream instream) throws exception {// bytearrayoutputstream outstream = new bytearrayoutputstream (); byte [] buffer = new byte [1024]; int Len =-1; // get the byte data while (LEN = instream. read (buffer ))! =-1) {outstream. write (buffer, 0, Len);} // obtain byte Data byte [] DATA = outstream. tobytearray (); outstream. close (); instream. close (); return new string (data);} 4. This example is just a simple operation. Composition Reading and writing of parts is not perfect in many places. I hope that interested netizens can communicate with each other ~~~
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.