标签:
一.知识了解:
1.什么是数据流?
二.书写代码:
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测试:
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异常。
标签:
原文地址:http://www.cnblogs.com/zhiyun930102/p/4562962.html