Android数据存储

一.知识了解:

1.什么是数据流?

      数据流(data stream)最初是通信领域使用的概念,代表传输中所使用的信息的数字编码信号序列。然而,我们所提到的数据流概念与此不同。这个概念最初在1998年由Henzinger在文献87中提出,他将数据流定义为“只能以事先规定好的顺序被读取一次的数据的一个序列”。数据流分为输入流(InputStream)和输出流(OutputStream)两类。
2.什么是输入流?
     在Java中,能够读取一个字节序列的对象就称作一个输入流。输入流只能读不能写,可从键盘或文件中获得数据。
3.什么是输出流?
     输出流可向显示器、打印机或文件中传输数据。

二.书写代码:

1.书写Model层,也就是FileService:

 1 package com.example.fileoperatedemo.service;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.BufferedWriter;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 import java.io.InputStreamReader;
10 import java.io.OutputStream;
11 import java.io.OutputStreamWriter;
12 
13 import android.content.Context;
14 
15 public class FileService {
16     private Context context;
17     private String fileName;
18     public FileService(Context context,String fileName){
19         this.context=context;
20         this.fileName=fileName;
21     }
22     public boolean save(String content){
23         BufferedWriter bw=null;// 缓冲区声明 
24         boolean isSaveSucceed=false;
25         try {
26             FileOutputStream fos=context.openFileOutput(fileName,context.MODE_PRIVATE);//创建输出流 
27             OutputStreamWriter writer=new OutputStreamWriter(fos);//创建读写器 
28             bw=new BufferedWriter(writer);//创建一个使用默认大小输出缓冲区的缓冲字符输出流 
29               bw.write(content);    
30               isSaveSucceed=true;
31               
32         } catch (FileNotFoundException e) {
33             e.printStackTrace();
34         } catch (IOException e) {
35             e.printStackTrace();
36         }finally{
37            if(bw!=null)
38             try {
39                 bw.close();
40             } catch (IOException e) {
41                 e.printStackTrace();
42             }
43         }
44         return isSaveSucceed;
45     }
46     
47     public String read(){
48         String line;
49         StringBuilder sb=new StringBuilder(); //用于添加数据
50         BufferedReader br=null;
51         try {
52             FileInputStream fis=context.openFileInput(fileName);//创建文件流 
53             br=new BufferedReader(new InputStreamReader(fis));//创建读写器 
54             while((line=br.readLine())!=null){
55                 sb.append(line);
56             }
57             
58         } catch (FileNotFoundException e) {
59             e.printStackTrace();
60         } catch (IOException e) {
61             e.printStackTrace();
62         }finally{
63             if(br!=null){
64                 try {
65                     br.close();
66                 } catch (IOException e) {
67                     // TODO Auto-generated catch block
68                     e.printStackTrace();
69                 }
70             }
71         }
72         return sb.toString();
73     }
74 
75 }

 

2.写测试类,即FileServiceTest.java

 1 package com.example.fileoperatedemo.test;
 2 
 3 import com.example.fileoperatedemo.service.FileService;
 4 
 5 import android.test.AndroidTestCase;
 6 
 7 public class FileServieTest extends AndroidTestCase {
 8     public void testSave(){
 9         FileService fileService=new FileService(getContext(), "test.txt");
10         fileService.save("赶紧去写作业!!!");
11     }
12 }

 

3.进行配置清单文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.fileoperatedemo"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.example.fileoperatedemo.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <uses-library android:name="android.test.runner"/>
26     </application>
27 
28     <instrumentation
29         android:name="android.test.InstrumentationTestRunner"
30         android:targetPackage="com.example.fileoperatedemo" >
31     </instrumentation>
32 
33 </manifest>

4.进行Android测试:

 
5.测试结果,成功:
 
6.当测试结果成功后,书写控制层代码:
 1 package com.example.fileoperatedemo;
 2 
 3 import com.example.fileoperatedemo.service.FileService;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.view.Menu;
 8 import android.view.View;
 9 import android.widget.EditText;
10 import android.widget.Toast;
11 //控制层
12 public class MainActivity extends Activity {
13     private EditText etContent;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         
20         initViews();
21     }
22 
23     private void initViews() {
24         etContent=(EditText) findViewById(R.id.etContent);
25         
26     }
27 
28     @Override
29     public boolean onCreateOptionsMenu(Menu menu) {
30         // Inflate the menu; this adds items to the action bar if it is present.
31         getMenuInflater().inflate(R.menu.main, menu);
32         return true;
33     }
34 
35     
36     public void save(View view){
37         //从V获取数据
38         String content=etContent.getText().toString();
39         //调用模型层M进行处理
40         FileService fileService=new FileService(this, "data.txt");
41         boolean isSavesucceed=fileService.save(content);
42         if(isSavesucceed){
43             Toast.makeText(this, "恭喜你,保存成功了", Toast.LENGTH_LONG).show();
44         }
45         
46     }
47 }

三.总结:

      从这个案例中,我们可以总结出以下几点:

      1:要熟练掌握Android MVC设计模式;

      2:要清晰Android文件操作的基本思路;

      3:要会处理JAVA异常。

posted on 2015-06-09 11:28  空空空空白的缘分  阅读(247)  评论(0编辑  收藏  举报

导航