Serialize an Object to a File with Java

On March 29, 2013, in Java, Programming, by LuCuS

Serialize an object to a file with Java.

	public static void serializeObject (Object object, String fileName)
    {
    	try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
			oos.writeObject(object);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

And to test it…

	public static void main(String[] args) {
		MyObject myObject = new MyObject();
		serializeObject(myObject, "C:\\path\\to\\my\\file.dat");
	}

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply