Android Object Serialization saveObjectAsFile(String filename, Object object)

Here you can find the source of saveObjectAsFile(String filename, Object object)

Description

save Object As File

License

Apache License

Declaration

public static void saveObjectAsFile(String filename, Object object) 

Method Source Code

/*/*from  www  .  j  av a 2  s .com*/
Copyright (c) 2012 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.FileChannel.*;
import java.util.*;
import java.util.zip.*;

public class Main{
    private static HashMap<String, String> serial;
    public static final String SERIALPREFIX = "SER:";
    public static void saveObjectAsFile(String filename, Object object) {
        if (filename.startsWith(SERIALPREFIX)) {
            filename = serialFileName(filename);
        }
        File f = new File(filename);
        try {
            ensureWriteable(f);
            OutputStream os = null;
            os = new BufferedOutputStream(new FileOutputStream(f));
            if (filename.endsWith(".gz")) {
                os = new GZIPOutputStream(os);
            }
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(object);
            oos.close();
        } catch (Exception e) {
            throw new Error(e);
        }
    }
    public static String serialFileName(String serId) {
        if (serId.startsWith(SERIALPREFIX)) {
            serId = serId.substring(SERIALPREFIX.length());
        }
        return serialProperties().get(serId);
    }
    /**
     * Ensures that the directories for the file exist
     * @param f
     */
    public static void ensureWriteable(File f) {
        File parent = f.getParentFile();
        if (parent != null) {
            parent.mkdirs();
        }
    }
    private static HashMap<String, String> serialProperties() {
        if (serial == null) {
            Properties serialP = PropertyLoader
                    .loadProperties("serialization");
            HashMap<String, String> serialM = new HashMap<String, String>();
            for (Map.Entry<Object, Object> e : serialP.entrySet()) {
                if (e.getKey() instanceof String
                        && e.getValue() instanceof String) {
                    serialM.put((String) e.getKey(), (String) e.getValue());
                } else {
                    System.err.println("Bad property: " + e);
                }
            }
            //do replacement on the properties
            serial = new HashMap<String, String>();
            for (Map.Entry<String, String> e : serialM.entrySet()) {
                String path = e.getValue();
                for (Map.Entry<String, String> oe : serialM.entrySet()) {
                    if (e.getKey() != oe.getKey()) {
                        path = path.replace("${" + oe.getKey() + "}",
                                oe.getValue());
                    }
                }
                serial.put(e.getKey(), path);
            }
        }
        return serial;
    }
}

Related

  1. loadObjectFromFile(String filename)
  2. readObjectFromFile(File fileLocation)
  3. readObjectFromFile(String filename)
  4. writeObjectToFile(File fileLocation, Object obj)
  5. writeObjectToFile(Serializable obj, String filename)
  6. objectToString(Object o)
  7. stringToObject(String s)