Examples of Android file storage Data Mode _android

Source: Internet
Author: User
Tags save file

In general, there are three ways to store data: One is a file, one is a database, and the other is a network. Here is a description of how the Android file is stored in this article.

1. File storage data used Java in the IO operation to save and read files, but android in the context class to encapsulate the input flow and output stream acquisition methods.

The stored files you create are saved under the/data/data/<package Name>/files folder.

2. Operation.

Save file Contents: Get output stream via Context.openfileoutput, parameters are file name and storage mode respectively.
Reads the file contents: Gets the input stream through the Context.openfileinput, the parameter is the file name.
Delete file: Context.deletefile Deletes the specified file, parameter is the name of the file that will be deleted.
Gets the list of file names: Gets all the file name arrays in the files directory by context.filelist.

* Ways to get file paths:

Absolute path:/data/data/<package name>/files/filename

Context:Context.getFilesDir () can get to "/data/data/<package name>/files"

3. Four types of File save mode.

Context.mode_private is the default mode of operation, which means that the file is private and can only be accessed by the application itself, and the content written in that mode overwrites the contents of the original file.

Context.mode_append Check if the file exists, append content to the file, or create a new file.
Mode_world_readable indicates that the current file can be read by another application.
Mode_world_writeable indicates that the current file can be written by another application.

In the use of mode, you can use "+" to select a variety of modes, such as Openfileoutput (FILENAME, context.mode_private + mode_world_readable);

The following program is used to demonstrate the use of the file store. Complete code Download: Android_files.rar

/** * mainactivity * * * @author zuolongsnail * */public class Mainactivity extends activity {private EditText WRI 
Teet; 
Private Button writebtn; 
Private TextView Contentview; 
public static final String FILENAME = "Setting.set"; 
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.main); 
Writeet = (edittext) Findviewbyid (R.id.write_et); 
WRITEBTN = (Button) Findviewbyid (R.ID.WRITE_BTN); 
Contentview = (TextView) Findviewbyid (R.id.contentview); 
Writebtn.setonclicklistener (New Operateonclicklistener ()); Class Operateonclicklistener implements Onclicklistener {@Override public void OnClick (View v) {writefiles (writeet 
. GetText (). toString ()); 
Contentview.settext (Readfiles ()); 
System.out.println (Getfilesdir ()); }///save file Contents private void Writefiles (String content) {try {//Open file Get output stream, file does not exist automatically create fileoutputstream fos = Openfil 
Eoutput (FILENAME, context.mode_private); Fos.write (content.getbytes()); 
Fos.close (); 
catch (Exception e) {e.printstacktrace (); 
Read the contents of the file private string Readfiles () {string content = null; 
try {fileinputstream FIS = openfileinput (FILENAME); 
Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); 
byte[] buffer = new byte[1024]; 
int len = 0; 
while (len = fis.read (buffer))!=-1) {baos.write (buffer, 0, Len); 
Content = Baos.tostring (); 
Fis.close (); 
Baos.close (); 
catch (Exception e) {e.printstacktrace (); 
} return content;  } 
}

Program screenshot:

Provides a tool class for storing data in a file:

/** 
* File storage Data Way Tool class 
* 
* @author zuolongsnail/public 
class Filesutil { 
/** 
* Save file Contents 
* 
* @param c 
* @param filename * 
@param 
content * Contents * 
/private void Writefiles ( Context C, string fileName, string content, int mode) 
throws Exception { 
//Open file Get output stream, file does not exist automatically create 
FileOutputStream fos = c.openfileoutput (fileName, mode); 
Fos.write (Content.getbytes ()); 
Fos.close (); 
} 
/** 
* Read the contents of the file * * 
@param c 
* @param filename * 
file name 
* @return return the contents 
of the file/private String Readfiles (context C, String FileName) throws Exception { 
Bytearrayoutputstream BAOs = new Bytearrayoutputstre AM (); 
FileInputStream FIS = c.openfileinput (fileName); 
byte[] buffer = new byte[1024]; 
int len = 0; 
while (len = fis.read (buffer))!=-1) { 
baos.write (buffer, 0, Len); 
} 
String content = baos.tostring (); 
Fis.close (); 
Baos.close (); 
return content; 
} 

Above through the example of the Android file storage data methods, I hope that the future work of everyone to help.

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.