使用 IO 读写文件(TXT)

Android 兼容 J2SE 中 I/O 的操作(InputStream, OutputStream, Reader, Writer),因此可以借助 I/O 读写应用程序或 SD 卡中的数据。保存数据的文件可以放在内存中,也可以放在 SD 卡中,放在手机内存中的文件都比较小,且经常读写;SD 卡中的数据文件较大,且这些文件的丢失不影响应用程序的正常运行。

Context 类提供了两种方法获取文件读取流和文件写入流。

1、打开文件获取读取数据流

abstract FileInputStream openFileInput(String name)

2、向文件写入数据,写入的方式由 mode 决定

abstract FileOutputStream openFileOutput(String name, int mode)

mode 的取值为:

1、MODE_PRIVATE 应用程序独占文件,不允许其他应用程序读写;

2、MODE_APPEND 向已有文件追加内容;

3、MODE_WORLD_READABLE 其他应用程序可以读取;

4、MODE_WORLD_WRITEABLE 其他应用程序可以读写。

例:

  1 import java.io.BufferedReader;
  2 import java.io.FileInputStream;
  3 import java.io.FileNotFoundException;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStreamReader;
  7 import java.io.PrintWriter;
  8 
  9 import android.os.Bundle;
 10 import android.app.Activity;
 11 import android.content.Context;
 12 import android.content.SharedPreferences;
 13 import android.content.SharedPreferences.Editor;
 14 import android.view.Menu;
 15 import android.view.View;
 16 import android.view.View.OnClickListener;
 17 import android.widget.Button;
 18 import android.widget.EditText;
 19 import android.widget.TextView;
 20 
 21 public class MainActivity extends Activity implements OnClickListener{
 22     
 23     EditText et1,et2;
 24     Button b1,b2;
 25     TextView tv1;
 26     
 27     @Override
 28     protected void onCreate(Bundle savedInstanceState) {
 29         super.onCreate(savedInstanceState);
 30         
 31         setContentView(R.layout.activity_main);
 32         et1 = (EditText) findViewById(R.id.et_name);
 33         et2 = (EditText) findViewById(R.id.et_pwd);
 34         tv1 = (TextView) findViewById(R.id.content);
 35         b1 = (Button) findViewById(R.id.bt_save);
 36         b2 = (Button) findViewById(R.id.bt_read);
 37         b1.setOnClickListener(this);
 38         b2.setOnClickListener(this);
 39     }
 40     
 41     @Override
 42     public void onClick(View view) {
 43         
 44         switch(view.getId()){
 45         case R.id.bt_save:
 46             save();
 47             et1.setText("");
 48             et2.setText("");
 49             break;
 50         case R.id.bt_read:
 51             read();
 52             break;
 53         }
 54     }
 55     
 56     // 读取文件
 57     private void read() {
 58         
 59         BufferedReader br = null;
 60         FileInputStream fis = null;
 61         
 62         try {
 63             tv1.setText("");
 64             fis = openFileInput("content.txt");
 65             br = new BufferedReader(new InputStreamReader(fis));
 66             String ln = null;
 67             while( (ln =  br.readLine()) != null ){
 68                 tv1.append(ln+"\n");
 69             }
 70         } catch (FileNotFoundException e) {
 71             // TODO Auto-generated catch block
 72             e.printStackTrace();
 73         } catch (IOException e) {
 74             // TODO Auto-generated catch block
 75             e.printStackTrace();
 76         }finally{
 77             if(br != null)
 78                 try {
 79                     br.close();
 80                 } catch (IOException e) {
 81                     // TODO Auto-generated catch block
 82                     e.printStackTrace();
 83                 }
 84         }
 85     }
 86     
 87     // 保存文件
 88     private void save() {
 89         
 90         if(et1.getText().toString().length()<1||et2.getText().toString().length()<1)return;
 91         FileOutputStream fos = null;
 92         PrintWriter pw = null;
 93         
 94         try {
 95             // 追加方式打开文件 content.txt
 96             fos = openFileOutput("content.txt", Context.MODE_APPEND);
 97             pw = new PrintWriter(fos);
 98             pw.println("好友姓名:" + et1.getText().toString() + "\t 电话号码:" + et2.getText().toString());
 99             pw.flush();
100         } catch (FileNotFoundException e) {
101             // TODO Auto-generated catch block
102             e.printStackTrace();
103         }finally{
104             if(pw!=null)pw.close();
105         }
106     }
107 }
MainActivity

布局文件 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10     
11     <TextView
12         android:id="@+id/tv_name"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="好友姓名:" />
16     <EditText 
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:id="@+id/et_name"
20         android:layout_toRightOf="@id/tv_name"
21         />
22     <TextView
23         android:id="@+id/tv_pwd"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:text="电话号码:"
27         android:layout_below="@id/et_name"
28          />
29     <EditText 
30         android:layout_width="match_parent"
31         android:layout_height="wrap_content"
32         android:id="@+id/et_pwd"
33         android:layout_below="@id/et_name"
34         android:layout_alignLeft="@id/et_name"
35         android:inputType="number"
36         />
37     <LinearLayout 
38         android:id="@+id/ln_op"
39         android:orientation="horizontal"
40         android:layout_width="fill_parent"
41         android:layout_height="wrap_content"
42         android:gravity="center_horizontal"
43         android:layout_below="@id/et_pwd"
44         >
45         <Button 
46             android:id="@+id/bt_save"
47             android:layout_width="wrap_content"
48             android:layout_height="wrap_content"
49             android:text="添加"
50             />
51         <Button 
52             android:id="@+id/bt_read"
53             android:layout_width="wrap_content"
54             android:layout_height="wrap_content"
55             android:text="查看"
56             />
57     </LinearLayout>
58     <TextView 
59             android:layout_width="fill_parent"
60             android:layout_height="wrap_content"
61             android:id="@+id/tv_tit"
62             android:text="已有联系人:"
63             android:layout_below="@id/ln_op"
64             />
65 
66     <ScrollView
67         android:layout_width="fill_parent"
68         android:layout_height="match_parent"
69         android:layout_below="@id/tv_tit" >
70 
71         <TextView 
72             android:layout_width="fill_parent"
73             android:layout_height="wrap_content"
74             android:id="@+id/content"
75             />
76         
77     </ScrollView>
78     
79     
80 </RelativeLayout>
activity_main.xml

添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

posted @ 2015-05-25 10:41  壬子木  阅读(300)  评论(0)    收藏  举报