Use Files for data storage:
Activity provides the openfileoutput () method to output data to a file. The specific implementation process is the same as saving data to a file in the j2se environment.
Public class fileactivity extends activity {
@ Override public void oncreate (bundle savedinstancestate ){
...
Fileoutputstream outstream = This. openfileoutput ("itcast.txt", context. mode_private );
Outstream. Write ("Chuanzhi podcast". getbytes ());
Outstream. Close ();
}
}
The first parameter of the openfileoutput () method is used to specify the file name. It cannot contain the path separator "/". If the file does not exist, Android will automatically create it. The created file is saved in the/data/<package name>/Files directory, for example,/data/CN. itcast. action/files/itcast.txt, click "window"-"Show View"-"other" in the eclipse menu, expand the android folder in the dialog box, and select the following file
In the file explorer view, expand the/data/<package name>/Files directory to view the file.
The second parameter of the openfileoutput () method is used to specify the operation mode. There are four modes: context. mode_private = 0
Context. mode_append = 32768
Context. mode_world_readable = 1
Context. mode_world_writeable = 2
Context. mode_private:The default operation mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file, if you want to append the newly written content to the original file. You can use context. mode_append.
Context. mode_append:Mode: Check whether the file exists. If yes, append the content to the file. Otherwise, create a new file.
Context. mode_world_readable and context. mode_world_writeable are used to control whether other applications have the permission to read and write the file.
Mode_world_readable: Indicates that the current file can be read by other applications;Mode_world_writeable: Indicates that the current file can be written by other applications.
If you want the file to be read and written by other applications, you can pass in:
Openfileoutput ("itcast.txt", context. mode_world_readable + context. mode_world_writeable );
Androidhas a set of security models. When the application program (.apk) is installed, the system will assign a userid to it. When the application wants to access other resources, such as files, it needs to match the userid. By default, files created by any application, sharedpreferences, and databases, should be private (in/data/<package name>/files), and cannot be accessed by other programs. Unless context. mode_world_readable or context. mode_world_writeable is specified during creation
Only in this way can other programs access the service correctly.
Store the file in sdcard:
Use the openfileoutput () method of activity to save files. The files are stored in the mobile phone space. Generally, the mobile phone storage space is not very large, and it is okay to store some small files, it is not feasible to store large files such as videos. For large files like videos, we can store them in sdcard. What does sdcard do? You can think of it as a mobile hard disk or a USB flash disk.
To use sdcard in the simulator, You need to first create an sdcard (of course, not a real sdcard, but an image file ). You can create sdcard along with the simulator created in eclipse, or use the doscommand to create it, as shown below:
Enter the tools directory in the android SDK installation path in the DOS window, and enter the following command to create an sdcard with a capacity of 2 GB. The file suffix can be obtained at will. IMG is recommended:
Mksdcard 2048 m d:/androidtool/sdcard. img
To access sdcard in a program, you need to apply for the permission to access sdcard.
The permission to access sdcard in androidmanifest. XML is as follows:
<! -- Create and delete file permissions in sdcard -->
<Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/>
<! -- Write data to sdcard -->
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>
Use SAX, Dom, or pull to parse XML files:
SaxIt is an XML parser with high resolution speed and low memory usage. It is very suitable for Android and other mobile devices. An event-driven file is used to parse an XML file. That is to say, it does not need to parse a complete file. In the process of parsing a document in order of content, sax checks whether the currently read characters are valid for a part of the XML syntax. If yes, the event is triggered.
DomWhen parsing an XML file, all the content of the XML file is read to the memory, and then you can use the dom api to traverse the XML tree and retrieve the required data. The code that uses Dom to operate XML looks intuitive and is simpler than the implementation based on sax in some aspects. However, because the Dom needs to read all the content of the XML file into the memory, the memory consumption is relatively large, especially for mobile devices running Android, because the device resources are precious, we recommend that you use SAX to parse XML files. Of course, it is feasible to use Dom if the content of the XML file is small.
PullThe parser runs in a similar way as the SAX Parser. It provides similar events, such as the start element and end element events. You can use parser. Next () to enter the next element and trigger the corresponding event. The event is sent as a numeric code, so you can use a switch to process the event you are interested in. When parsing an element, call parser. nexttext () to obtain the value of the next text element.