现在的位置: 首页 > 综合 > 正文

大话企业级android读书笔记(三)

2012年10月31日 ⁄ 综合 ⁄ 共 1856字 ⁄ 字号 评论关闭

android中的数据存储类型包括:内存,普通文件,Shared Preferences,XML和SQLLite等

文件操作:

包括读写

/**
 * 【文件操作】
 */
package Iwit.IwitTest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.content.Context;
import android.widget.Toast;

/**
 * @author Shine
 *【文件读,写】
 */
public class FileOperate
{  
 /**
  * 【读取文件】
  * @param context
  * @param fileName
  * @return
  */
 public String ReadText(Context context,String fileName)
 {
       FileInputStream fIn = null;
       InputStreamReader isr = null;      
       char[] inputBuffer = new char[255];
       String data = null;      
       try
       {
        fIn =context.openFileInput(fileName);     
           isr = new InputStreamReader(fIn);
           isr.read(inputBuffer);
           data = new String(inputBuffer);
           Toast.makeText(context, "read Succeed",Toast.LENGTH_SHORT).show();
       }
      catch (Exception e)
          {     
       e.printStackTrace();
       Toast.makeText(context, " not read",Toast.LENGTH_SHORT).show();
          }
       finally
       {
              try {
                     isr.close();
                     fIn.close();
                   }
              catch (IOException e)
              {
                    e.printStackTrace();
              }
           }
       return data;
   }
 
 /**
  * 【写入文件】
  * @param context
  * @param fileName
  * @param data
  */
 public void WriteText(Context context, String fileName,String data)
 {
       FileOutputStream fOut = null;
       OutputStreamWriter osw = null;
      
       try{
        fOut =context.openFileOutput(fileName, 1);
           osw = new OutputStreamWriter(fOut);
           osw.write(data);
           osw.flush();
           Toast.makeText(context, " saved",Toast.LENGTH_SHORT).show();
           }
           catch (Exception e)
           {     
           e.printStackTrace();
           Toast.makeText(context, " not saved",Toast.LENGTH_SHORT).show();
           }
           finally
           {
              try {
                     osw.close();
                     fOut.close();
                   }
              catch (IOException e)
               {
                    e.printStackTrace();
                 }
           }
  }
}

关于android操作sdcard的细节,详看

http://www.ylmf.net/zhuanti/zt02/2010/1130/12389.html

接下来是共享参数的操作,这个适合于将一些配置信息存储到这个位置上,可以存储小量的数据

http://www.cnblogs.com/over140/archive/2011/01/13/1934301.html

介绍的很详细

接下来是android操作关于sqllite的操作,

http://www.cnblogs.com/TerryBlog/archive/2010/06/12/1757166.html

连示例都有了,可以下来测试

然后是内容提供访问者ContentProvider

主要是用到多个程序之间对外提供一个可访问的接口,通过统一的uri对外提供访问

http://www.cnblogs.com/linzheng/archive/2011/01/22/1942101.html

抱歉!评论已关闭.