Here you can find the source of writeObjectToFile(Object o, String filename)
Parameter | Description |
---|---|
o | Object to be written to file |
filename | Name of the temp file |
Parameter | Description |
---|---|
IOException | If can't write file. |
public static File writeObjectToFile(Object o, String filename) throws IOException
//package com.java2s; import java.io.*; import java.util.zip.GZIPOutputStream; public class Main { /**// w ww .ja va 2 s . co m * Write object to a file with the specified name. The file is silently gzipped if the filename ends with .gz. * * @param o Object to be written to file * @param filename Name of the temp file * @throws IOException If can't write file. * @return File containing the object */ public static File writeObjectToFile(Object o, String filename) throws IOException { return writeObjectToFile(o, new File(filename)); } /** * Write an object to a specified File. The file is silently gzipped if the filename ends with .gz. * * @param o Object to be written to file * @param file The temp File * @throws IOException If File cannot be written * @return File containing the object */ public static File writeObjectToFile(Object o, File file) throws IOException { return writeObjectToFile(o, file, false); } /** * Write an object to a specified File. The file is silently gzipped if the filename ends with .gz. * * @param o Object to be written to file * @param file The temp File * @param append If true, append to this file instead of overwriting it * @throws IOException If File cannot be written * @return File containing the object */ public static File writeObjectToFile(Object o, File file, boolean append) throws IOException { // file.createNewFile(); // cdm may 2005: does nothing needed OutputStream os = new FileOutputStream(file, append); if (file.getName().endsWith(".gz")) { os = new GZIPOutputStream(os); } os = new BufferedOutputStream(os); ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(o); oos.close(); return file; } }