文件存储

目标效果:

  

程序运行,显示一输入框和按钮,在输入框输入内容点击按钮会提示保存成功,关闭程序,再次打开会在输入框显示刚才输入的内容,并提示英文。


1.activity_main.xml页面放置两个控件。

activity_main.xml页面:

  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.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/etInput"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_above="@+id/btClick"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginBottom="34dp"  
  14.         android:ems="10"  
  15.         android:hint="请输入想保存的内容" >  
  16.   
  17.         <requestFocus />  
  18.     </EditText>  
  19.   
  20.     <Button  
  21.         android:id="@+id/btClick"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_centerHorizontal="true"  
  25.         android:layout_centerVertical="true"  
  26.         android:onClick="save"  
  27.         android:text="确定" />  
  28.   
  29. </RelativeLayout>  


2.新建FileService.Java类,编写保存和读取方法。

FileService.java页面:

  1. package com.example.file;  
  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.OutputStreamWriter;  
  11.   
  12. import android.content.Context;  
  13.   
  14. public class FileService {  
  15.     private Context context;  
  16.     public FileService(Context context){  
  17.         this.context=context;  
  18.     }  
  19.   
  20.     /*创建文件保存内容*/  
  21.     public void save(String fileName,String content){  
  22.         BufferedWriter bw=null;  
  23.         try {  
  24.             //打开文件,得到文件输出流(字节流),参数一为文件名,参数二为打开方式,为私有  
  25.             FileOutputStream fos=context.openFileOutput(fileName,context.MODE_PRIVATE);  
  26.             //OutputStreamWriter把字节流转换成字符流,BufferedWriter创建缓冲区  
  27.             bw=new BufferedWriter(new OutputStreamWriter(fos));  
  28.             //往缓冲区写入内容  
  29.             bw.write(content);  
  30.         } catch (FileNotFoundException e) {  
  31.             e.printStackTrace();  
  32.         }catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         }finally{  
  35.             try {  
  36.                 if(bw!=null){  
  37.                     bw.close();  
  38.                 }  
  39.             } catch (IOException e) {  
  40.                 e.printStackTrace();  
  41.             }  
  42.         }  
  43.     }  
  44.       
  45.     /*打开文件读取内容*/  
  46.     public String read(String fileName){  
  47.         String line;  
  48.         StringBuilder sb=new StringBuilder();  
  49.         BufferedReader br=null;  
  50.         try {  
  51.             FileInputStream fis=context.openFileInput(fileName);  
  52.             br=new BufferedReader(new InputStreamReader(fis));  
  53.             while((line=br.readLine())!=null){  
  54.                 sb.append(line);  
  55.             }  
  56.         } catch (FileNotFoundException e) {  
  57.             e.printStackTrace();  
  58.         }catch (IOException e) {  
  59.             e.printStackTrace();  
  60.         }finally{  
  61.             try {  
  62.                 if(br!=null){  
  63.                     br.close();  
  64.                 }  
  65.             } catch (IOException e) {  
  66.                 e.printStackTrace();  
  67.             }  
  68.         }  
  69.         return sb.toString();  
  70.     }  
  71. }  


3.写完保存方法和读取方法,可以先不写MainActivity.java的代码,先写一个fileText.java类进行单元测试,检测保存方法和读取方法是否有错误。

fileText.java页面:

  1. package com.example.text;  
  2.   
  3. import com.example.file.FileService;  
  4.   
  5. import android.test.AndroidTestCase;  
  6. import android.util.Log;  
  7.   
  8. public class fileText extends AndroidTestCase{  
  9.   
  10.     public void saveText(){  
  11.         FileService fileservice=new FileService(getContext());  
  12.         fileservice.save("out.txt","hello!");  
  13.     }  
  14.     public void readText(){  
  15.         FileService fileservice=new FileService(getContext());  
  16.         String s=fileservice.read("out.txt");  
  17.         Log.i("MainActivity","s="+s);  
  18.     }  
  19. }  


4.编写完测试类后,开始测试运行,两个方法的运行方式都一样。


5.测试运行后,如果在左上角出现绿色横条,说明方法编写无错误。

6.最后编写MainActivity.java页面,用于将保存和读取方法和控件绑定。
MainActivity.java页面:
  1. package com.example.file;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.View;  
  6. import android.widget.EditText;  
  7. import android.widget.Toast;  
  8.   
  9. public class MainActivity extends Activity {  
  10.   
  11.     private EditText etInput;  
  12.       
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.           
  18.         /*获取控件方法*/  
  19.         getId();  
  20.         /*读取文件内容方法*/  
  21.         read();  
  22.     }  
  23.   
  24.     /*读取文件内容方法*/  
  25.     private void read() {  
  26.         FileService fileservice=new FileService(this);  
  27.         String content=fileservice.read("myfile.txt");  
  28.         if(!content.isEmpty()){  
  29.             etInput.setText(content);  
  30.             etInput.setSelection(content.length());  
  31.             Toast.makeText(this,"Restoring succeeded",Toast.LENGTH_SHORT).show();  
  32.         }  
  33.     }  
  34.       
  35.     /*点击按钮调用save方法*/  
  36.     public void save(View view){  
  37.         FileService fileservice=new FileService(this);  
  38.         String content=etInput.getText().toString();  
  39.         fileservice.save("myfile.txt",content);  
  40.         Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();  
  41.     }  
  42.   
  43.     /*获取控件方法*/  
  44.     private void getId() {  
  45.         etInput=(EditText) findViewById(R.id.etInput);  
  46.     }  
  47. }  


7.另外在Afest.xml页面添加配置信息。


8.运行后可以显示目标效果,可以从环境中看到文件保存目录,还是需要在模拟器环境下查看。



9.按照以前博文介绍的方法,可以导保存到桌面查看文件中的内容,可以发现out.txt文件中是测试时保存的“hello!”,myfile.txt文件中是运行时输入的“123456”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值