Android 中保存数据到文件中

1、在安卓开发中,会遇到保存数据到手机中以及从手机中获取数据的情况

/**
     * 把数据存放到手机内存中
     * 
     * @param number
     * @param password
     * @return
     */
    public static boolean saveUserInfo(Context context, String number,
            String password) {

        try {

            // getCacheDir()方法用于获取/data/data/<package name>/cache目录 缓存数据
            // getFilesDir()方法用于获取/data/data/<package name>/files目录

            // 定义路径
            // String path = "/data/data/com.example.qqlogin/qqlogin.txt";

            File filesDir = context.getFilesDir();
            // 动态获得路径
            File file = new File(filesDir, "qqlogin.txt");

            // 输出流,把数据输出到文件中
            FileOutputStream fos = new FileOutputStream(file);
            // 要写入的数据
            String data = number + "##" + password;

            // 写入字节流
            fos.write(data.getBytes());

            // 清空缓存
            fos.flush();
            // 关闭流
            fos.close();

            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 从文件中读取数据,并返回出去
     * 
     * @return
     */
    public static Map<String, String> getUserInfo(Context context) {
        // String path = "/data/data/com.example.qqlogin/qqlogin.txt";
        // 动态获取文件名
        File filesDir = context.getFilesDir();
        // 动态获得路径
        File f = new File(filesDir, "qqlogin.txt");

        try {
            // 从文件中读取流
            // FileInputStream fis = new FileInputStream(path);
            FileInputStream fis = new FileInputStream(f);
            // 把字节流 转换为 字符串流
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    fis));
            String text = reader.readLine();
            if (!TextUtils.isEmpty(text)) {
                String[] spilt = text.split("##");
                Map<String, String> userInfoMap = new HashMap<String, String>();
                userInfoMap.put("number", spilt[0]);
                userInfoMap.put("password", spilt[1]);
                return userInfoMap;
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

2、通过SharedPreferences 向手机中写入数据

/**
     * 把数据存放到手机内存中
     * 
     * @param number
     * @param password
     * @return
     */
    public static boolean saveUserInfo(Context context, String number,
            String password) {
        try {

            // 文件保存在 /data/data/包名/shared_prefs/itheima28
            SharedPreferences sp = context.getSharedPreferences("aa",
                    context.MODE_PRIVATE);

            // 获得一个编辑对象
            Editor ed = sp.edit();

            // 存放数据
            ed.putString("number", number);
            ed.putString("password", password);
            // 提交数据
            ed.commit();
            return true;

        } catch (Exception e) {

        }

        return false;
    }

    /**
     * 从文件中读取数据,并返回出去
     * 
     * @return
     */
    public static Map<String, String> getUserInfo(Context context) {

        try
        {
        // 文件保存在 /data/data/包名/shared_prefs/itheima28
        SharedPreferences sp = context.getSharedPreferences("aa",context.MODE_PRIVATE);        
        String number=sp.getString("number", null);
        String password=sp.getString("password", null);
        
        Map<String,String>  userInfoMap=new HashMap<String, String>();
        userInfoMap.put("number", number);
        userInfoMap.put("password",password);
        return  userInfoMap;    
        }
        catch(Exception e)
        {
            
        }
        
        return null;
    }
原文地址:https://www.cnblogs.com/luoyangcn/p/4694605.html