zoukankan      html  css  js  c++  java
  • 大话企业级android读书笔记(三)

    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

    作者:张锋
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
    更多精彩文章可以观注
    微信公众号 soft张三丰

    微信交流群,添加群主微信,邀请入群
  • 相关阅读:
    配置hbase
    hive配置
    scala及spark配置
    Eclipse 配置hadoop
    腾讯云部署hadoop
    助教总结
    预习非数值数据的编码方式
    预习原码补码
    学习java的第六周
    作业一总结
  • 原文地址:https://www.cnblogs.com/skyme/p/2219273.html
Copyright © 2011-2022 走看看