Android TXT file read/write (read resource file, read private and SD file method)

Source: Internet
Author: User

Import Java.io.FileInputStream; Import Java.io.FileOutputStream; Import Java.io.InputStream; Import Org.apache.http.util.EncodingUtils;  Import android.app.Activity; public class FileAccess {/** *, file access under Private folder (/data/data/package name/files) * * @param fileName * @param mess Age */public void Writefiledata (string fileName, String message) {try {FileOutputStream fou             t = openfileoutput (fileName, mode_private);             byte[] bytes = Message.getbytes ();             Fout.write (bytes);         Fout.close ();         } catch (Exception e) {e.printstacktrace (); }}/** *//Read file under./data/data/Package Name/files/* * @param fileName * @return */public String R         Eadfiledata (String fileName) {String res = "";             try {fileinputstream fin = openfileinput (fileName);             int length = fin.available ();             byte[] buffer = new Byte[length]; Fin.read (buffer);            res = encodingutils.getstring (buffer, "UTF-8");         Fin.close ();         } catch (Exception e) {e.printstacktrace ();     } return res; }/** * Write, read sdcard directory files, to use FileOutputStream, can not be used Openfileoutput * different points: Openfileoutput is compiled in raw, Fileoutputstrea M is any file can be * @param fileName * @param message *///////write the file under the/mnt/sdcard/directory public void Writefilesdcard (S Tring filename, String message) {try {//FileOutputStream Fout = openfileoutput (filename, Mode_priva             TE);             FileOutputStream fout = new FileOutputStream (fileName);             byte[] bytes = Message.getbytes ();             Fout.write (bytes);         Fout.close ();         } catch (Exception e) {e.printstacktrace ();         }}//Read the file under the/mnt/sdcard/directory public string Readfilesdcard (string fileName) {String res = ""; try {fileinputstream fin = new FileInputStream (fileName);             int length = fin.available ();             byte[] buffer = new Byte[length];             Fin.read (buffer);             res = encodingutils.getstring (buffer, "UTF-8");         Fin.close ();         } catch (Exception e) {e.printstacktrace ();     } return res; }/** * Second, get the file from the Raw folder in resource and read the data (resource file can only read and write) * * @param fileinraw * @return */Public S         Tring Readfromraw (int fileinraw) {String res = "";             try {InputStream in = Getresources (). Openrawresource (Fileinraw);             int length = in.available ();             byte[] buffer = new Byte[length];             In.read (buffer);             res = encodingutils.getstring (buffer, "GBK");             res = new String (buffer, "GBK");         In.close ();         } catch (Exception e) {e.printstacktrace ();     } return res; }/** * Three, get the file from the asset and read the data (resource file can only read and write) * * @param fileName    * @return */public string Readfromasset (string fileName) {String res = "";             try {InputStream in = Getresources (). Getassets (). open (FileName);             int length = in.available ();             byte[] buffer = new Byte[length];             In.read (buffer);         res = encodingutils.getstring (buffer, "UTF-8");         } catch (Exception e) {e.printstacktrace ();     } return res; }    }



Android TXT file read/write (read resource file, read private and SD file method)

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.