Here you can find the source of writeObject(File file, Object object)
Parameter | Description |
---|---|
file | the file |
object | the object to be serialized to file |
Parameter | Description |
---|---|
IOException | Any exception thrown by the underlying OutputStream. |
InvalidClassException | Something is wrong with a class used by serialization. |
NotSerializableException | the object to be serialized does not implement the java.io.Serializable interface. |
public static void writeObject(File file, Object object) throws IOException, InvalidClassException, NotSerializableException
//package com.java2s; /*/*from w ww . ja v a 2s. co m*/ * net/balusc/util/FileUtil.java * * Copyright (C) 2007 BalusC * * This program is free software: you can redistribute it and/or modify it under the terms of the * GNU Lesser General Public License as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library. * If not, see <http://www.gnu.org/licenses/>. */ import java.io.Closeable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InvalidClassException; import java.io.NotSerializableException; import java.io.ObjectOutputStream; public class Main { /** * writes the spcified object to the file specified * @param file the file * @param object the object to be serialized to file * @throws IOException Any exception thrown by the underlying OutputStream. * @throws InvalidClassException Something is wrong with a class used by serialization. * @throws NotSerializableException the object to be serialized does not implement the java.io.Serializable interface. */ public static void writeObject(File file, Object object) throws IOException, InvalidClassException, NotSerializableException { mkdirs(file); ObjectOutputStream output = null; try { output = new ObjectOutputStream(new FileOutputStream(file)); output.writeObject(object); } finally { close(output, file); } } /** * Check and create missing parent directories for the given file. * @param file The file to check and create the missing parent directories for. * @throws IOException If the given file is actually not a file or if creating parent * directories fails. */ private static void mkdirs(File file) throws IOException { if (file.exists() && !file.isFile()) { throw new IOException("File " + file.getPath() + " is actually not a file."); } File parentFile = file.getParentFile(); if (!parentFile.exists() && !parentFile.mkdirs()) { throw new IOException("Creating directories " + parentFile.getPath() + " failed."); } } /** * Close the given I/O resource of the given file. * @param resource The I/O resource to be closed. * @param file The I/O resource's subject. */ private static void close(Closeable resource, File file) { if (resource != null) { try { resource.close(); } catch (IOException e) { String message = "Closing file " + file.getPath() + " failed."; // Do your thing with the exception and the message. Print it, log it or mail it. System.err.println(message); e.printStackTrace(); } } } /** * Close the given I/O resource. * @param resource The I/O resource to be closed. */ public static void close(Closeable resource) { if (resource != null) { try { resource.close(); } catch (IOException e) { System.err.println("Closing resource failed!"); e.printStackTrace(); } } } }