主要通过流来完成

  得到输出流对象FileOutputStream:通过Context的openFileOuput("文件名",模式)方法获取

    文件路径:/data/data/<包名>/files/  存放在这个目录下
    模式有二:Activity.MODE_PRIVATE覆盖;Activity.MODE_APPEND——追加

  得到输入流对象FileInputStream:通过Context的openFileInput("文件名")返回一个输入流对象

    会自动在路径:/data/data/<包名>/files/  下找到要读取的文件名

 

  接下来的操作即为java的IO操作技术!

贴入一段小代码

  1 package com.example.filepersistencetest;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.BufferedWriter;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.InputStreamReader;
  8 import java.io.OutputStreamWriter;
  9 
 10 import android.app.Activity;
 11 import android.os.Bundle;
 12 import android.util.Log;
 13 import android.view.View;
 14 import android.view.View.OnClickListener;
 15 import android.widget.Button;
 16 import android.widget.EditText;
 17 
 18 public class MainActivity extends Activity implements OnClickListener {
 19     private EditText editText;
 20     private Button sava_button;
 21     private Button read_button;
 22 
 23     @Override
 24     protected void onCreate(Bundle savedInstanceState) {
 25         super.onCreate(savedInstanceState);
 26         setContentView(R.layout.activity_main);
 27         editText = (EditText) findViewById(R.id.edit);
 28         sava_button = (Button) findViewById(R.id.save);
 29         read_button = (Button) findViewById(R.id.read);
 30 
 31         // 绑定单击事件
 32         sava_button.setOnClickListener(this);
 33         read_button.setOnClickListener(this);
 34 
 35     }
 36 
 37     @Override
 38     public void onClick(View v) {
 39         // 监听
 40         switch (v.getId()) {
 41         case R.id.save:
 42             save();
 43             break;
 44         case R.id.read:
 45             read();
 46             break;
 47 
 48         default:
 49             break;
 50         }
 51 
 52     }
 53 
 54     public void save() {
 55         String inputText = editText.getText().toString();
 56         BufferedWriter bufw = null;
 57         FileOutputStream out = null;
 58         // 得到输出流 Context->openFileOutput("文件名",写出模式——覆盖Private或者追加append)
 59         // 返回FileOutputStream对象
 60         try {
 61 
 62             out = openFileOutput("my_data.txt", Activity.MODE_PRIVATE);
 63             bufw = new BufferedWriter(new OutputStreamWriter(out));
 64             bufw.write(inputText);
 65             Log.d("test", inputText);
 66             bufw.flush();
 67             editText.setText("");
 68         } catch (Exception e) {
 69             throw new RuntimeException("打开文件异常!!");
 70         } finally {
 71             try {
 72                 if (null != bufw)
 73                     bufw.close();
 74             } catch (Exception e2) {
 75                 throw new RuntimeException("关闭流失败");
 76             }
 77         }
 78     }
 79 
 80     public void read() {
 81         editText.setText("");
 82         FileInputStream in = null;
 83         BufferedReader bur = null;
 84         try {
 85             in = openFileInput("my_data.txt");
 86             bur = new BufferedReader(new InputStreamReader(in));
 87             String line = null;
 88             boolean flag = false;
 89             while ((line = bur.readLine()) != null) {
 90                 if (flag)
 91                     editText.append("\n" + line);//
 92                 else {
 93                     editText.append(line);
 94                     flag=true;
 95                 }
 96             }
 97         } catch (Exception e) {
 98             // TODO: handle exception
 99         } finally {
100             try {
101                 bur.close();
102             } catch (Exception e2) {
103                 // TODO: handle exception
104             }
105         }
106 
107     }
108 
109 }
filepersistence

相关文章: