First of all to introduce how to store data using files, activity provides the Openfileoutput () method can be used to output data to a file, the implementation process and in the J2SE environment to save data to the file is the same.
public void Save ()
{
try {
FileOutputStream outstream=this.openfileoutput ("A.txt", context.mode_world_readable);
Outstream.write (Text.gettext (). toString (). GetBytes ());
Outstream.close ();
Toast.maketext (Myactivity.this, "Saved", Toast.length_long). Show ();
} catch (FileNotFoundException e) {
Return
}
catch (IOException e) {
return;
}
}
The first parameter of the Openfileoutput () method specifies the file name, cannot contain the path delimiter "/", and if the file does not exist, Android automatically creates it. The created file is saved in the/data/data/<package Name>/files directory, such as:/data/data/cn.itcast.action/files/itcast.txt, by clicking on the Eclipse menu " Window "-" Show View "-" other ", expand the Android folder in the Conversation window, select the File Explorer view below, and then expand/data/data/<package in the File Explorer view The file can be seen in the Name>/files directory.
The second parameter of the Openfileoutput () method is used to specify the mode of operation, with four modes: context.mode_private = 0
Context.mode_append = 32768
context.mode_world_readable = 1
Context.mode_world_writeable = 2
Context.mode_private: As the default mode of operation, which means that the file is private data and can only be accessed by the app itself, in which the content of the write overwrites the contents 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: The mode checks whether the file exists, appends content to the file, or creates a new file.
Context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the 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.
If you want the file to be read and written by another app, you can pass in:
Openfileoutput ("Itcast.txt", context.mode_world_readable + context.mode_world_writeable);
Android has its own security model, and when the application (. apk) is installed, the system assigns him a userid, which requires a UserID match when the app is going to access other resources such as files. By default, any file created by the app, Sharedpreferences, should be private (in/data/data/<package name>/files) and other programs cannot access it. Unless you specify context.mode_world_readable or context.mode_world_writeable at creation time, only such other programs can access them correctly.
Read File contents
public void Load ()
{
try {
FileInputStream instream=this.openfileinput ("A.txt");
Bytearrayoutputstream stream=new Bytearrayoutputstream ();
Byte[] Buffer=new byte[1024];
int length=-1;
while ((Length=instream.read (buffer))!=-1) {
Stream.Write (buffer,0,length);
}
Stream.Close ();
Instream.close ();
Text.settext (Stream.tostring ());
Toast.maketext (Myactivity.this, "Loaded", Toast.length_long). Show ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
catch (IOException e) {
return;
}
}
For a private file to be accessed only by the app that created the file, if you want the file to be read and written by another app, you can specify context.mode_world_readable and Context.mode_world_writeable permissions when you create the file.
The activity also provides the Getcachedir () and Getfilesdir () methods:
The Getcachedir () method is used to get the/data/data/<package Name>/cache directory
The Getfilesdir () method is used to get the/data/data/<package name>/files directory
Put the file into the SD card
Use the activity of the Openfileoutput () method to save the file, the file is stored in the mobile phone space, the general phone storage space is not very large, storage of small files is OK, if you want to store large files such as video, it is not feasible. For large files like video, we can store it in SDcard. What is sdcard for? You can think of it as a removable hard drive or USB stick.
To use SDcard in the simulator, you need to first create a SDcard card (not really sdcard, just the image file). Creating a sdcard can be created when eclipse creates the emulator, or it can be created with a DOS command, as follows:
In the DOS window, enter the tools directory of the Android SDK installation path and enter the following command to create a sdcard with a capacity of 2G, the file suffix can be arbitrarily taken, it is recommended to use. IMG:
Mksdcard 2048M D:\AndroidTool\sdcard.img
To access SDcard in the program, you need to request permission to access SDcard.
The permissions to add access to SDcard in Androidmanifest.xml are as follows:
<!--Create and delete files in SDcard permissions--
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--write data to sdcard permissions--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
To store files to SDcard, the program must first determine whether the phone is equipped with sdcard and can read and write.
Note: Access to SDcard must include access to SDcard in Androidmanifest.xml
if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {
File Sdcarddir = Environment.getexternalstoragedirectory ();//Get SDcard Directory
File SaveFile = new file (Sdcarddir, "a.txt");
FileOutputStream OutStream = new FileOutputStream (saveFile);
Outstream.write ("Test". GetBytes ());
Outstream.close ();
}
The Environment.getexternalstoragestate () method is used to get the state of the sdcard, and if the phone is loaded with sdcard and can read and write, the method returns a state equal to Environment.media_ Mounted.
The Environment.getexternalstoragedirectory () method is used to get the SDcard directory, and of course to get the SDcard directory, you can also write:
File Sdcarddir = new file ("/sdcard"); Get SDcard Directory
File SaveFile = new file (Sdcarddir, "itcast.txt");
The above two lines of code can be synthesized one sentence: file SaveFile = new file ("/sdcard/a.txt");
FileOutputStream OutStream = new FileOutputStream (saveFile);
Outstream.write ("Test". GetBytes ());
Outstream.close ();
File storage for Android data storage