在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。

数据读取

public static String read(Context context, String file) {String data = "";try {FileInputStream stream = context.openFileInput(file);StringBuffer sb = new StringBuffer();int c;while ((c = stream.read()) != -1) {sb.append((char) c);}stream.close();data = sb.toString();} catch (FileNotFoundException e) {} catch (IOException e) {}return data;}

从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package中共享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package。

数据写入

public static void write(Context context, String file, String msg) {try {FileOutputStream stream = context.openFileOutput(file,Context.MODE_WORLD_WRITEABLE);stream.write(msg.getBytes());stream.flush();stream.close();} catch (FileNotFoundException e) {} catch (IOException e) {}}

在这里打开文件的时候,声明了文件打开的方式。

一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:

public static Properties load(Context context, String file) {Properties properties = new Properties();try {FileInputStream stream = context.openFileInput(file);properties.load(stream);} catch (FileNotFoundException e) {} catch (IOException e) {}return properties;}public static void store(Context context, String file, Properties properties) {try {FileOutputStream stream = context.openFileOutput(file,Context.MODE_WORLD_WRITEABLE);properties.store(stream, "");} catch (FileNotFoundException e) {} catch (IOException e) {}}
   
/Chapter09_Data_02/src/com/amaker/test/MainActivity.java
                      代码            
                                  package             com.amaker.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
private static final String FILE_NAME = " temp.txt " ;
private Button b1,b2;
private EditText et1,et2;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
b1
= (Button)findViewById(R.id.Button01);
b2
= (Button)findViewById(R.id.Button02);

et1
= (EditText)findViewById(R.id.EditText01);
et2
= (EditText)findViewById(R.id.EditText02);

b1.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
write(et1.getText().toString());
}
});

b2.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
et2.setText(read());
}
});
}

private String read(){
try {
FileInputStream fis
= openFileInput(FILE_NAME);
byte [] buffer = new byte [fis.available()];
fis.read(buffer);
return new String(buffer);
}
catch (Exception e) {
e.printStackTrace();
}
return null ;
}

private void write(String content){
try {
FileOutputStream fos
= openFileOutput(FILE_NAME, MODE_APPEND);
fos.write(content.getBytes());
fos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

/Chapter09_Data_02/res/layout/main.xml
                      代码            
                                  <?            xml version            =            "            1.0            "             encoding            =            "            utf-8            "            ?>            
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation
= " vertical " android:layout_width = " fill_parent "
android:layout_height
= " fill_parent " >

< TextView android:layout_width = " fill_parent "
android:layout_height
= " wrap_content " android:text = " File Test " />

< EditText android:text = "" android:id = " @+id/EditText01 "
android:layout_width
= " fill_parent " android:layout_height = " wrap_content " android:height = " 100px " ></ EditText >

< Button android:id = " @+id/Button01 " android:layout_width = " wrap_content "
android:layout_height
= " wrap_content " android:text = " Write " ></ Button >

< EditText android:text = "" android:id = " @+id/EditText02 "
android:layout_width
= " fill_parent " android:layout_height = " wrap_content " android:height = " 100px " ></ EditText >

< Button android:id = " @+id/Button02 " android:layout_width = " wrap_content "
android:layout_height
= " wrap_content " android:text = " Read " ></ Button >

</ LinearLayout >

/Chapter09_Data_02/AndroidManifest.xml
                      代码            
                                  <?            xml version="1.0" encoding="utf-8"            ?>            
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package
="com.amaker.test"
android:versionCode
="1"
android:versionName
="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".MainActivity"
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 ="3" />

</ manifest >



更多相关文章

  1. android 判断是白天还是晚上,然后设置地图模式
  2. Sqlite在Android上的一个Bug - 临时文件写策略
  3. Android示例HelloGallery中R.styleable unresolved的解决办法
  4. Android多国语言文件夹
  5. 关于android studio 出现Error:Execution failed for task ':app
  6. Android(安卓)Snippet
  7. android UEventObserver的用法
  8. Android文件操作的模式
  9. Logger详解(二)

随机推荐

  1. Android(安卓)程式开发:(一)详解Activity —
  2. android makefile(android.mk)分析(序)
  3. 想抢先体验Android操作系统的魅力吗?那就
  4. Android事件分发机制详解:史上最全面、最
  5. 与Android有关的三起诉讼事件
  6. Android使用libjpeg实现图片压缩
  7. 2011Android技术面试整理附有详细答案(包
  8. android之NFC基础技术分享
  9. Android(安卓)Service创建USB HOST通信
  10. Android(安卓)4.0应用界面设计分析