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 ;}}