Here you can find the source of writeObjectToFile(Object obj, String filePath)
public static void writeObjectToFile(Object obj, String filePath)
//package com.java2s; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Main { public static void writeObjectToFile(Object obj, String filePath, boolean append) { if (obj == null || filePath == null || filePath.length() == 0) return; try {// www. j a v a2 s .co m File file = new File(filePath); if (!file.exists()) { File parent = file.getParentFile(); if (!parent.exists()) parent.mkdirs(); boolean isSuc = file.createNewFile(); System.out.println("isSuc" + isSuc); } ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(file, append)); oos.writeObject(obj); oos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void writeObjectToFile(Object obj, String filePath) { writeObjectToFile(obj, filePath, false); } }