The example in this article describes how Android uses files for data storage. Share to everyone for your reference. Specifically as follows:
Many times we develop software that needs to store the processed data for a second visit . Android provides the following ways for data storage:
File
Sharedpreferences (Parameters)
SQLite database
Contents provider (content Provider)
Internet
First of all to introduce how to use files to store data
The activity provides a openfileoutput () method that can be used to output data to a file, and the implementation process is the same as saving data to a file in a J2SE environment
public class Fileactivity extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
FileOutputStream OutStream = this.openfileoutput ("Ljq.txt", context.mode_private);
Outstream.write ("My name is Lin Yi-chin". GetBytes ());
Outstream.close ();
}
Openfileoutput (fileName, Mode) method detailed:
First parameter:
used to specify a file name and cannot contain the path separator "/". If the file does not exist, Android will automatically create it. The created file is saved in the/data/data/<packagename>/files directory, such as:/data/data/com.ljq.activity/files/itcast.txt, by clicking MyEclipse Menu " Window "-" Show View "-" other ",
Expand the Android folder in the Conversation window, select the File Explorer view below, and then expand the/data/data/<package name>/files directory in the File Explorer view to see it
Second parameter:
For specifying the operation mode, there are four modes
context.mode_private = 0
The default mode of operation, which means that the file is private and can only be accessed by the application itself, in which the written content 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 = 32768
Check to see if the file exists, append content to the file, or create a new file.
context.mode_world_readable = 1
Indicates that the current file can be read by another application
Context.mode_world_writeable = 2
Indicates that the current file can be written by another application
Attention:
Context.mode_world_readable and context.mode_world_writeable are used to control whether other applications have permission to read and write to the file.
If you want the file to be read and written by other applications, you can pass in: Openfileoutput ("Ljq.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, and when the application accesses other resources such as files, it needs userid matching. By default, any file created by the application, sharedpreferences, should be private (located in/data/data/<package name>/files) and not accessible by other programs. Unless context.mode_world_readable or context.mode_world_writeable are specified at the time of creation, only such other programs can access them correctly.
If you want to open a file that is private to the/data/data/<package name>/files directory, you can use the activity to provide the Openfileinput () method.
FileInputStream instream = This.getcontext (). Openfileinput ("Ljq.txt");
or use the absolute path of the file directly:
File File = new file ("/data/data/com.ljq.action/files/ljq.txt");
FileInputStream instream = new FileInputStream (file);
Note: the "com.ljq.action" in the file path above is the package where the application resides, and when you write the code you should replace the package you used for your own application.
The private file can only be accessed by the application that created the file, and if you want the file to be read and written by another application, when you create the file,
Specify context.mode_world_readable and Context.mode_world_writeable permissions.
The activity also provides 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
Case
Fileservice class: File Access Operation class
package com.ljq.service; import java.io.ByteArrayOutputStream; import
Java.io.InputStream;
Import Java.io.OutputStream; public class Fileservice {/** * Save data * * @param outputstream * @param content * @throws Exception/Publi
c static void Save (OutputStream outputstream, String content) throws Exception {outputstream.write ());
Outputstream.close (); /** * Read Data * * @param inputstream * @return * @throws Exception/public static String read (InputStream
InputStream) throws Exception {//write data to memory Bytearrayoutputstream Bytearrayoutputstream = new Bytearrayoutputstream ();
Buffers byte[] buffer = new byte[1024];
int len =-1;
while (len = inputstream.read (buffer))!=-1) {bytearrayoutputstream.write (buffer, 0, Len);
}///Storage data byte[] = Bytearrayoutputstream.tobytearray ();
Bytearrayoutputstream.close ();
Inputstream.close ();
return new String (data); }
}
Fileservicetest Test class:
Package com.ljq.service;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Android.content.Context;
Import Android.test.AndroidTestCase;
Import Android.util.Log;
/**
* Android Test * *
@author Jiqinlin * */Public
class Fileservicetest extends Androidtestcase {
private final String TAG = "Fileservicetest";
public void Testsave () throws exception{
outputstream outputstream = This.getcontext (). Openfileoutput ("Ljq.txt", Context.mode_private);
Fileservice.save (OutputStream, "abc");
}
public void Testread () throws exception{
InputStream inputstream= this.getcontext (). Openfileinput ("Ljq.txt");
String content = Fileservice.read (inputstream);
LOG.I (TAG, content);
}
Manifest file:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
package=" com.ljq.activity "
android:versioncode=" 1 "
android:versionname=" 1.0 ">
< Application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<uses-library android:name= "Android.test.runner"/>
<activity android:name=. Fileactivity "
android:label=" @string/app_name ">
<intent-filter>
<action android:name=" Android.intent.action.MAIN "/>
<category android:name=" Android.intent.category.LAUNCHER "/>"
</intent-filter>
</activity>
</application>
<uses-sdk android:minsdkversion = "7"/>
<instrumentation android:name= "Android.test.InstrumentationTestRunner"
android: Targetpackage= "com.ljq.activity" android:label= "Tests for my App"/>
I hope this article will help you with your Android program.