Many times the software we develop needs to store the processed data.For another visit.Android provides the following methods for data storage:


sharedpreferences (parameters)

sqlite database

Content provider

The internet

First, I will show you how to use files to store data.

The activity provides the openfileoutput () method which can be used 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 {
 public void oncreate (bundle savedinstancestate) {
   fileoutputstream outstream=this.openfileoutput ("ljq.txt", context.mode_private);
   outstream.write ("My name is Lin Jiqin" .getbytes ());
   outstream.close ();

The openfileoutput (filename, mode) method is explained in detail:

First parameter:

Used to specify the file name,Cannot include path separator "/". If the file does not exist,android will create it automatically.The created files are saved in the/data/data//files directory, such as:/data/data/com.ljq.activity/files/itcast.txt, by clicking the myeclipse menu "window"-"show view"- "Other",

Expand the android folder in the dialog window, select the file explorer view below, and then expand the/data/data//files directory in the file explorer view to see the file

The second parameter:

Used to specify the operating mode,There are four modes


You can use context.mode_append.


Check if the file exists,Add content to the file if it exists,Otherwise, a new file is created.


Indicates that the current file can be read by other applications


Indicates that the current file can be written by other applications


context.mode_world_readable and context.mode_world_writeable are used to control whether other applications have permission to read and write the file.

If i want the file to be read and written by other applications,Can be passed in:openfileoutput ("ljq.txt", context.mode_world_readable + context.mode_world_writeable);

android has its own set of security models,When the application (.apk) is installed, the system will assign him a userid. When the application wants to access other resources such as files,You need to match userid. by default,Files created by any application,sharedpreferences, the database should be private (located at/data/data /<package name>/files), and cannot be accessed by other programs.Unless context.mode_world_readable or context.mode_world_writeable is specified at creation time, only then can other programs correctly access it.

If i want to open a private file stored in the/data/data//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:"com.ljq.action" in the above file path is the package where the application is located,When you write code, you should replace the package used by your own application.

Private files can only be accessed by the app that created the file,If i want the file to be readable and writable by other applications,When creating a 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//cache directory

The getfilesdir () method is used to get the/data/data//files directory


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
  * /
 public static void save (outputstream outputstream, string content) throws exception {
  outputstream.write (content.getbytes ());
  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 ();
  byte [] buffer=new byte [1024];
  int len ​​= -1;
  while ((len=inputstream.read (buffer))!=-1) {
   bytearrayoutputstream.write (buffer, 0, len);
  //Storing data
  byte [] data=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"
 <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">
    <action android:name="android.intent.action.main" />
    <category android:name="android.intent.category.launcher" />
 <uses-sdk android:minsdkversion="7" />
 <instrumentation android:name="android.test.instrumentationtestrunner"
   android:targetpackage="com.ljq.activity" android:label="tests for my app" />
  • Previous How to find the server is slow
  • Next iOS-UIButton (UIEdgeInsets)/Set the text and image on the button to be vertically centered