Android的文件操作


目前,机会搜有的设备都会涉及到文件的操作,例如什么电脑,手机等设备。Android的文件操作和电脑是比较类似的,既可以存储在手机内置的存储器里也可以是sd卡。在这次的博客里主要介绍在手机内置存储器里的文件操作。

一.开发流程

(1)界面的设计

(2)设计android的业务层

(3)单元测试

(4)设置android的控制器层

二.开发步骤

(1)设计软件界面

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/filename"  
  11.     />  
  12.  <EditText  
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"   
  15.     android:id="@+id/filename"  
  16.     />  
  17.    <TextView    
  18.     android:layout_width="fill_parent"   
  19.     android:layout_height="wrap_content"   
  20.     android:text="@string/content"  
  21.       
  22.     />  
  23.  <EditText  
  24.     android:layout_width="fill_parent"   
  25.     android:layout_height="wrap_content"   
  26.     android:id="@+id/content"  
  27.     android:minLines="3"  
  28.     />  
  29.     <Button  
  30.      android:layout_width="wrap_content"   
  31.      android:layout_height="wrap_content"   
  32.      android:text="@string/button"  
  33.      android:id="@+id/button"/>  
  34. </LinearLayout>  
这里也把R文件给大家看看
  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY. 
  2.  * 
  3.  * This class was automatically generated by the 
  4.  * aapt tool from the resource data it found.  It 
  5.  * should not be modified by hand. 
  6.  */  
  7.   
  8. package org.lxh.file;  
  9.   
  10. public final class R {  
  11.     public static final class attr {  
  12.     }  
  13.     public static final class drawable {  
  14.         public static final int icon=0x7f020000;  
  15.     }  
  16.     public static final class id {  
  17.         public static final int button=0x7f050002;  
  18.         public static final int content=0x7f050001;  
  19.         public static final int filename=0x7f050000;  
  20.     }  
  21.     public static final class layout {  
  22.         public static final int main=0x7f030000;  
  23.     }  
  24.     public static final class string {  
  25.         public static final int app_name=0x7f040001;  
  26.         public static final int button=0x7f040004;  
  27.         public static final int content=0x7f040003;  
  28.         public static final int failure=0x7f040006;  
  29.         public static final int filename=0x7f040002;  
  30.         public static final int hello=0x7f040000;  
  31.         public static final int success=0x7f040005;  
  32.     }  
  33. }  
(2)设计业务层
  1. package org.lxh.service;  
  2.   
  3.   
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.FileInputStream;  
  6.   
  7. import java.io.FileOutputStream;  
  8.   
  9. import android.content.Context;  
  10. import android.util.Log;  
  11.   
  12. public class FileService {  
  13.     private Context context;  
  14.   
  15.     public FileService(Context context) {  //通过构造方法传入context   
  16.         this.context = context;  
  17.     }  
  18.     //保存文件   
  19.     public void saveFile(String filename,String content) throws Exception{  //异常交给调用处处理   
  20.         FileOutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);  
  21.         out.write(content.getBytes());  
  22.         out.close();  
  23.     }  
  24.     public String readFile(String filename) throws Exception{  //异常交给调用处处理   
  25.         FileInputStream in=context.openFileInput(filename);  
  26.         byte b[]=new byte[1024];  
  27.         int len=0;  
  28.         ByteArrayOutputStream array=new ByteArrayOutputStream();  
  29.           
  30.         while((len=in.read(b))!=-1){  //开始读取文件   
  31.             array.write(b,0,len);  
  32.         }  
  33.         byte data[]=array.toByteArray(); //把内存里的数据读取出来   
  34.           
  35.         in.close();  //每个流都必须关闭   
  36.         array.close();  
  37.         return new String(data);  //把byte数组转换为字符串并返回   
  38.     }  
  39. }  
  • 1
  • 2
  • 下一页

相关内容