Here you can find the source of writeObjectToFile(Serializable obj, String filename)
Parameter | Description |
---|---|
obj | the object to write |
filename | the file to write it to |
public static boolean writeObjectToFile(Serializable obj, String filename)
//package com.java2s; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; public class Main { /**// www . j av a 2 s . co m * Write a serializable object to a file * @param obj the object to write * @param filename the file to write it to * @return true iff successful */ public static boolean writeObjectToFile(Serializable obj, String filename) { //serialize the object try { //use buffering OutputStream file = new FileOutputStream(filename); OutputStream buffer = new BufferedOutputStream(file); ObjectOutput output = new ObjectOutputStream(buffer); try { output.writeObject(obj); } finally { output.close(); } } catch (IOException ex) { ex.printStackTrace(); return false; } return true; } }