Read and Write Android TXT files

Source: Internet
Author: User
Package COM. wirelessqa. helper; 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 extends activity {/*** 1. File Access in a private folder (/data/package name/files) ** @ Param filename * @ Param message */Public void writefiledata (string filename, string message) {try {fileoutputstream fout = openfileoutput (filename, mode_private ); byte [] bytes = message. getbytes (); fout. write (bytes); fout. close ();} catch (exception e) {e. printstacktrace () ;}}/*** // read the file in. /data/package name/files/The following ** @ Param filename * @ return */Public String readfiledata (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 the files in the sdcard directory, use fileoutputstream, cannot use openfileoutput * difference: openfileoutput is compiled in raw, fileoutputstream is a public void writefilesdcard (string filename, string message) file in the/mnt/sdcard/directory of * @ Param message * // For any file) {try {// fileoutputstream fout = openfileoutput (filename, mode_private); fileoutputstream fout = new fileoutputstream (filename); byte [] bytes = message. getbytes (); fout. write (bytes); fout. close ();} catch (exception e) {e. printstacktrace () ;}/// read the Public String readfilesdcard (string filename) {string res = "" in the/mnt/sdcard/directory ""; 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;}/*** 2. Obtain the file from the raw folder in the resource and read the data (the resource file can only be read but cannot be written) ** @ Param fileinraw * @ return */Public String 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;}/*** 3. Obtain the file from the asset and read the data (resource files can only be read but cannot be written) ** @ 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 ;}}

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.