Here you can find the source of serialize(Object object, String fileName)
public static boolean serialize(Object object, String fileName) throws IOException
//package com.java2s; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main { /**// w ww.jav a 2s . c om * Serialize an object into a file if the file does not exist yet */ public static boolean serialize(Object object, String fileName) throws IOException { File file = new File(fileName); if (file.exists()) return false; else { FileOutputStream fos = null; BufferedOutputStream bos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); oos = new ObjectOutputStream(bos); oos.writeObject(object); oos.flush(); return true; } catch (IOException e) { file.delete(); throw e; } finally { if (oos != null) oos.close(); if (bos != null) bos.close(); if (fos != null) fos.close(); } } } }