Android中数据储存方式的几种工具类

一.在Android开发中数据的存储方式有五种:

  1.使用SharedPreferences进行存储:

     适用范围存储小量的数据,储存的格式有字符串类型及基本数据类型。一般用于存储应用程序的配置信息,如用户设置的App中的设置信息是否打开音效自动升级等等,它是基于XML文件存储的键值对(key-value)的数据。

  2.文件存储:通过打开数据文件的文件IO流进行读和写的操作,存储在手机本身的SD卡中或者手机的储存空间中。

  3.SQLite数据库储存:Android中数据库使用sqlite,支持SQL语言的语法。

  4.使用ContentProvider存储数据:一个程序可以通过实现一个ContentProvider的抽象接口将自己的数据完全暴露出去,而且ContentProvider是以类似数据库中表的方式将数据暴露的。那么外界获取其提供的数据,也就应该与从数据库中获取数据的操作基本一样,只不过是采用URL来表示外界需要访问的“数据库”。

  5.使用网络储存:我们可以调用WebService返回的数据或是解析HTTP协议实现网络数据交互。

二.工具类的使用:

  1.SharedPreferences的工具类:

package com.example.foreveross.clickdemo;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Foreveross on 2017/4/15.
 */
public class SPUtil {
    private static final String FILENAME ="mydata" ;           //设置获取数据期望的文件名字

    /**
     * 存入的字符串的数据,Context.MODE_PRIVATE一般是私有的也可是可读可写的模式
     * @param context
     * @param key           键
     * @param value         值
     */
    public static void putString(Context context, String key, String value){
        
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(key, value);
        edit.commit();
    }
    /**
     * 获取的字符串的数据,Context.MODE_PRIVATE一般是私有的也可是可读可写的模式
     * @param context
     * @param key           键
     * @param defValue      默认值
     */
    public static String getString(Context context,String key,String defValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, defValue);
    }

    //存入布尔值数据
    public static void putBoolean(Context context,String key,boolean value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }
    //获取布尔值数据
    public static boolean getBoolean(Context context,String key,boolean defValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defValue);
    }

    public static void putFloat(Context context,String key,float value){
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putFloat(key, value);
        edit.commit();
    }

    public static float getFloat(Context context,String key,float defValue){
        SharedPreferences sharedPreferences = context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        return sharedPreferences.getFloat(key, defValue);
    }
}
在使用SharePreferences时无非就是SharePreferences.Editor的put和commit的方法。

  2.文件存储的工具类:

package com.example.foreveross.clickdemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Foreveross on 2017/4/15.
 */
public class FileUtil {
    /**
     * 不存在则创建文件
     * @param path
     * @return
     */
    public static String createIfNotExist(String path){
        File file=new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();       //如果不存在则创建新的file
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        return path;
    }
    /**
     * 向文件中写入数据 
     *
     * @param filePath 目标文件全路径 
     * @param data 要写入的数据 
     * @return true表示写入成功  false表示写入失败 
     */
    public static boolean writeBytes(String filePath, byte[] data) {
        try {
            FileOutputStream fos = new FileOutputStream(filePath);      //文件输出流
            fos.write(data);
            fos.close();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }

    /**
     * 从文件中读取数据 
     *
     * @param file
     * @return
     */
    public static byte[] readBytes(String file) {
        try {
            FileInputStream fis = new FileInputStream(file);
            int len = fis.available();
            byte[] buffer = new byte[len];
            fis.read(buffer);
            fis.close();
            return buffer;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return null;

    }

    /**
     * 向文件中写入字符串String类型的内容 
     * @param file 文件路径 
     * @param content 文件内容 
     * @param charset  写入时候所使用的字符集 
     */
    public static void writeString(String file, String content, String charset) {
        try {
            byte[] data = content.getBytes(charset);
            writeBytes(file, data);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    /**
     * 从文件中读取数据,返回类型是字符串String类型 
     * @param file  文件路径 
     * @param charset 读取文件时使用的字符集,如utf-8、GBK等 
     * @return
     */
    public static String readString(String file, String charset) {
        byte[] data = readBytes(file);
        String ret = null;

        try {
            ret = new String(data, charset);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ret;
    }

}
 手机里的一些有用的方法:

1).getCacheDir()方法用于获取/data/data/<application package>/cache目录。

2).getFilesDir()方法用于获取/data/data/<application package>/files目录。

3).Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录。

4).Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录。

5).获取SD卡的路径:



    public String getSDPath(){  
    File sdDir = null;  
    boolean sdCardExist = Environment.getExternalStorageState()  
    .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在  
    if (sdCardExist)  
    {  
    sdDir = Environment.getExternalStorageDirectory();//获取跟目录  
    }  
    return sdDir.toString();  
    }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值