Data storage and access common ways: File Sharedpreferences (preference parameter setting) SQLite database Contents provider (content Provider) Internet Activity (context) The Context.getcachedir () method is used to get the/data/data/<package Name>/cache directory The Context.getfilesdir () method is used to get the/data/data/<package name>/files directory The activity (context) provides a openfileoutput (Filename,mode) method for outputting data to a file; The first parameter is used to specify a file name and cannot contain the path separator '/' The second parameter is operation mode: Context.mode_private: Private operation mode created files can only be accessed by this program, if the file does not exist, will be automatically created, in addition: the contents of the written file will overwrite the contents of the original file; Context.mode_append: Mode checks whether the file exists, appends the content if it exists, or creates a new file; Context.mode_readable: Indicates that the current file can be read by other applications; Context.mode_writeable: Indicates that the current file can be written by another application; If you want the file to be read and written by another application: Incoming (context.mode_readable+context.mode_writeable) FileOutputStream OutStream = Context.openfileoutput (filename,context.mode_private); Example Write Data public void WriteFile (String filename,string writestr{ try{ FileOutputStream fout =openfileoutput (FileName, mode_private); byte [] bytes = Writestr.getbytes (); Fout.write (bytes); Fout.close (); } catch (Exception e) { E.printstacktrace (); } } Reading data public string ReadFile (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; } param file name, how to operate Android has its own security model, and when the application (. apk) is installed, the system is assigned to a userid, and when the application accesses other resources such as files, it is matched UserID, by default, any file created by the application. Sharedpreferences, the database is private (the created file is stored in the/data/data/<package name>/files directory), and only the specified mode of operation is externally readable or writable to be accessible by other programs; Read file: <1> FileInputStream instream = context.openfileinput (filename); LOG.I (Tag,instream ...) <2> Path= "/data/data/<package name>/files/hello.txt"; File File = new file (path); FileInputStream instream = new FileInputStream (file); LOG.I (Tag,instream ...) CTRL + SHIFT + X/Y case To read file data from raw resource: String res = ""; try{
To get the raw data stream in a resource InputStream in = Getresources (). Openrawresource (R.raw.test); Get the size of the data int length = in.available (); byte [] buffer = new Byte[length]; Reading data In.read (buffer); According to Test.txt encoding type to choose the appropriate encoding, if not adjusted will garbled res = encodingutils.getstring (buffer, "BIG5");
Shut down In.close (); }catch (Exception e) { E.printstacktrace (); } |