如何映射转换为字节,并保存到内部存储 [英] How to convert Map to Bytes and save to internal storage

查看:163
本文介绍了如何映射转换为字节,并保存到内部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能将我地图<整数,字符串>为字节[] ,然后把它写入内部存储器?我目前有:

  {尝试
            FOS的FileOutputStream = context.openFileOutput(Const.FILE_CATEGORIES,Context.MODE_PRIVATE);
            fos.write(NULL);
        }赶上(FileNotFoundException异常五){
            //重新加载,并重新创建文件
        }

But..I不知道如何让地图成正确的格式,然后去code将其返回到它的原始格式有一次我需要再次加载它。我需要重新每星期一次这个文件,并加载它在应用程序启动。


解决方案

  1. 在Java中使用的序列化可以轻松地分析任何可序列化
    反对字节流。尝试使用的ObjectInputStream和
    ObjectOuputStream。


  2. 使用JSON来恢复。您可以使用谷歌,GSON转换的Java
    对象JSON,反之亦然。


  3. 使用包裹中的机器人。类android.os.Parcel是designd来传递数据
    在android系统(活动,服务)的组件之间的,但你仍然可以用它来做数据的持久性。
    只要记住不要将数据发送到网络,因为differenct
    平台上可能有不同的算法做解析。


我写了系列化的演示,一试。

 公共静态无效的主要(字串[] args)抛出异常{
    //创建原始数据。
    地图<整数,字符串>数据=新的HashMap<整数,字符串>();
    data.put(1,你好);
    data.put(2,世界);
    的System.out.println(data.toString());    //转换映射到字节数组
    ByteArrayOutputStream byteOut =新ByteArrayOutputStream();
    ObjectOutputStream的出=新的ObjectOutputStream(byteOut);
    out.writeObject(数据);    //解析字节数组地图
    ByteArrayInputStream的byteIn =新ByteArrayInputStream进行(byteOut.toByteArray());
    在ObjectInputStream的=新的ObjectInputStream(byteIn);
    地图<整数,字符串>数据2 =(地图<整数,字符串>)in.readObject();
    的System.out.println(data2.toString());
}

How can I convert my Map<Integer, String> to byte[], and then write it to internal storage? I currently have:

        try {
            FileOutputStream fos = context.openFileOutput(Const.FILE_CATEGORIES, Context.MODE_PRIVATE);
            fos.write(null);
        } catch (FileNotFoundException e) {
            // reload and create the file again
        }

But..I don't know how to get the Map into the correct format, and then decode it back to it's original format once I need to load it again. I need to recreate this file once per week, and load it on application startup.

解决方案

  1. Using serialization in java you can easily parse any serializable objects to byte stream. Try to use the ObjectInputStream and the ObjectOuputStream.

  2. Use json to restore. You can use google-gson to convert Java objects to JSON and vice-versa.

  3. Use Parcel in android. The class android.os.Parcel is designd to pass data between the components in android(activity, service), but you can still use it to do data persistence. Just remember do not send the data to internet since differenct platforms may have different algorithm to do parsing.

I wrote a demo for serialization , have a try.

public static void main(String[] args) throws Exception {
    // Create raw data.
    Map<Integer, String> data = new HashMap<Integer, String>();
    data.put(1, "hello");
    data.put(2, "world");
    System.out.println(data.toString());

    // Convert Map to byte array
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(data);

    // Parse byte array to Map
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    Map<Integer, String> data2 = (Map<Integer, String>) in.readObject();
    System.out.println(data2.toString());
}

这篇关于如何映射转换为字节,并保存到内部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆