Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeObject.

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:io.cloudslang.content.httpclient.build.CookieStoreBuilder.java

public static byte[] serialize(Object obj) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    ObjectOutputStream o = new ObjectOutputStream(b);
    o.writeObject(obj);
    return b.toByteArray();
}

From source file:Main.java

public static byte[] serialize(Object obj) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream os;
    try {//  w  w w  . ja  v a 2s  . c o  m
        os = new ObjectOutputStream(out);
        os.writeObject(obj);
        return out.toByteArray();
    } catch (Exception e) {

        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void saveObj(Object obj, String fileName) throws IOException {
    File f = new File(fileName + ".tmp");
    f.getParentFile().mkdirs();/*from w w  w  .  j  ava 2 s.com*/
    FileOutputStream fos = new FileOutputStream(f);
    ObjectOutputStream out = new ObjectOutputStream(fos);
    out.writeObject(obj);
    out.flush();
    out.close();
    fos.flush();
    fos.close();
    File file = new File(fileName);
    file.delete();
    f.renameTo(file);
}

From source file:Main.java

private static void writePageNameHashesToFile(Map<UUID, String> map, File folder) {
    if (uuidsToNames != null) {
        String mapString = "Map: ";
        for (Map.Entry<UUID, String> entry : uuidsToNames.entrySet()) {
            mapString += entry.toString();
            mapString += "\n";
        }//  w ww. j a va  2 s.c om
    }

    if (map == null) { // TODO: Do I need this?
        map = new HashMap<UUID, String>();
    }

    try {
        FileOutputStream fos = new FileOutputStream(folder.getAbsolutePath() + fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(map);
        oos.close();
    } catch (Exception e) {
        Log.e("Write to file", e.toString());
    }
}

From source file:com.eviware.loadui.util.serialization.SerializationUtils.java

public static byte[] serialize(Object object) throws IOException {
    if (object == null)
        return null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(object);
    oos.flush();/*from   ww w.  jav a  2s  . c  o m*/

    return baos.toByteArray();
}

From source file:IO.serializer.java

public static void Serialize(Object object, String destinationFilename) {
    String serializedFilename = String.format("%s\\%s.%s", m_systemTempDirectory, destinationFilename,
            m_serializedFileExtension);//  w w  w. j  a v  a2 s.  c  om

    try (FileOutputStream fos = new FileOutputStream(serializedFilename)) {
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        oos.close();
    } catch (IOException ex) {
        Console.PrintLine(
                String.format("Error serilizing object '%s': %s", destinationFilename, ex.getMessage()), true,
                false);
    }
}

From source file:Main.java

public static void writeDataFile(String filename, Context context, Object object) {
    File file = new File(context.getDir("data", 0), filename);
    try {// w ww .  ja  v  a2s  .c  o m
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file, false));
        outputStream.writeObject(object);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Util.java

/**
  * Serializes an object to a file, masking out annoying exceptions.
  *  Any IO exceptions are caught, and printed to standard error.
  *  Consider using {@link #writeGzippedObject(java.io.File, java.io.Serializable)}
  *  instead, for that method will compress the serialized file, and it'll still
  * be reaoable by {@link #readObject}.//from   w w  w  .  j  a  v a  2 s.c o m
  * @param f File to write to
  * @param obj Object to serialize
  * @see #writeGzippedObject(java.io.File, java.io.Serializable)
  */
public static void writeObject(File f, Serializable obj) {
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
        oos.writeObject(obj);
        oos.close();
    } catch (IOException e) {
        System.err.println("Exception writing file " + f + ": " + e);
    }
}

From source file:Main.java

public static byte[] toBinary(Serializable obj) {
    if (obj == null) {
        return null;
    }//  ww w  .ja  v  a  2  s . c  o m
    if (obj instanceof byte[]) {
        return (byte[]) obj;
    }
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        try {
            return baos.toByteArray();
        } finally {
            oos.close();
        }
    } catch (Exception e) {
        String msg = "Failed to convert the object to binary: obj=" + obj;
        throw new IllegalStateException(msg, e);
    }
}

From source file:com.yfiton.oauth.OAuthUtils.java

public static void writeAuthorizationInfo(Path file, AuthorizationData data) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file));
    oos.writeObject(data);
    oos.close();/*from   ww  w  . j a  v  a 2s . c  o  m*/
}