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:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * Serialize a StackExchangeThread into a binary file
 * // www .  j a  v a  2s.c  o  m
 * @param threadToSerialize - The StackExchangeThread to be serialized
 * @return the path(relative to the resource folder) of the serialized binary file
 * @throws IngestionException
 */
public static String serializeThreadToBinFile(StackExchangeThread threadToSerialize, String dirPath)
        throws IngestionException {
    String binFileName = threadToSerialize.getId() + StackExchangeConstants.BIN_FILE_SUFFIX;
    try {
        String binFilePath = dirPath + binFileName;
        File serFile = new File(binFilePath);
        if (serFile.getParentFile() != null)
            serFile.getParentFile().mkdirs();
        if (!serFile.exists())
            serFile.createNewFile();
        OutputStream binOut = new FileOutputStream(serFile);
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(threadToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binFileName;
}

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a sample method that saves a handful of car instances as Serialized
 * objects, and as JSON objects./*  w  w w .ja  va  2s.co m*/
 */
public static void doBuildIt2Output() {
    Car[] cars = { new Car("Honda", "Civic", 2001), new Car("Buick", "LeSabre", 2003),
            new Car("Toyota", "Camry", 2005) };

    // Saves as Serialized Objects
    try {
        FileOutputStream objFile = new FileOutputStream("cars.obj");
        ObjectOutputStream objStream = new ObjectOutputStream(objFile);

        for (Car car : cars) {
            objStream.writeObject(car);
        }

        objStream.close();
        objFile.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Saves as JSONObject
    try {
        PrintWriter jsonStream = new PrintWriter("cars.json");

        JSONArray arr = new JSONArray();
        for (Car car : cars) {
            arr.add(car.toJSON());
        }

        JSONObject json = new JSONObject();
        json.put("cars", arr);

        jsonStream.println(json.toJSONString());

        jsonStream.close();
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:jedi.util.serialization.Pickle.java

/**
 * Serializes a object into a file.// ww  w  .  j  a v  a2 s  . co m
 * 
 * @param o Object to be serialized.
 * @param f File where the serialization will occurs.
 * @param append defines if the write will occur in append mode.
 */
public static void dump(Object o, File f, boolean append) {

    if (o != null && f != null) {

        try {
            // File where the data will be written.
            FileOutputStream fos = new FileOutputStream(f, append);

            // Object that writes (writer) the data on the file.
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.flush();
            oos.writeObject(o); // write the data.
            oos.close();

            fos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.delphix.session.util.AbstractExternalCodec.java

/**
 * Same as above except with length limit check.
 * @throws IOException/*from  w w w.j a v  a 2s  . com*/
 */
public static boolean isThrowableSerializable(Throwable cause, int limit) throws IOException {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    ObjectOutputStream output;

    try {
        output = new ObjectOutputStream(byteStream);
        output.writeObject(cause);
        return (limit <= 0 || byteStream.toByteArray().length <= limit);
    } catch (NotSerializableException e) {
        return false;
    } finally {
        ExceptionUtil.closeIgnoreExceptions(byteStream);
    }
}

From source file:Main.java

public static Object clone(Object object) throws Exception {
    Object copy = null;/*from   w  ww  .j a v  a 2s  .  co  m*/

    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        oos.flush();

        ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);
        copy = ois.readObject();
    } finally {
        if (ois != null)
            ois.close();
        if (oos != null)
            oos.close();
    }
    return copy;
}

From source file:com.ikon.util.Serializer.java

/**
 * @param obj//  w  w w .  j  ava2s.com
 */
public static byte[] write(Object obj) throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;

    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        baos.flush();
        return baos.toByteArray();
    } finally {
        IOUtils.closeQuietly(oos);
        IOUtils.closeQuietly(baos);
    }
}

From source file:com.yahoo.spaclu.data.extract.ExtractFeatureSpark.java

/**
 * @deprecated//www  .  ja v a 2s .  c  om
 */
public static boolean writeToHDFS(Object object, String fileName) {
    // Create a default hadoop configuration
    Configuration conf = new Configuration();
    // Specifies a new file in HDFS.
    Path filenamePath = new Path(fileName);

    try {
        // Parse created config to the HDFS
        FileSystem fs = FileSystem.get(conf);

        // if the file already exists delete it.
        if (fs.exists(filenamePath)) {
            throw new IOException();
        }

        FSDataOutputStream fos = fs.create(filenamePath);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(object);
        fos.close();
        oos.close();
        return true;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }
}

From source file:gov.nih.nci.ncicb.cadsr.bulkloader.ui.CommandLineUserInterfaceImpl.java

private static void serializeInput(UserInput userInput) {
    try {//from   ww w  .  jav  a 2  s . co m
        FileOutputStream fos = new FileOutputStream(SERIALIZE_FILE_NAME);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(userInput);
    } catch (Exception e) {
        log.error("Error serializing input", e);
    }
}

From source file:com.oprisnik.semdroid.utils.FileUtils.java

public static void writeObjectToFile(Object object, File file) throws IOException {
    if (!file.exists()) {
        file.getParentFile().mkdirs();//from   ww  w.  ja  v  a2s .  c om
    }
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        out.writeObject(object);
        out.flush();
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:hydrograph.engine.hadoop.inputformat.TupleMemoryInputFormat.java

public static void storeTupleGenerator(Configuration conf, String key, ITupleGenerator tupleGenerator,
        long numTuples) {
    LOG.debug("Storing ITupleGenerator: {}", tupleGenerator);

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutputStream out;

    try {//from   w  w  w.  java  2s  . c  o m
        out = new ObjectOutputStream(stream);
        out.writeObject(tupleGenerator);
        out.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    String confVal = numTuples + ":" + encodeBytes(stream.toByteArray());
    conf.set(key, confVal);

}