Here you can find the source of serializeFile(String path, Object o)
Parameter | Description |
---|---|
path | where the file should be saved |
o | the object, that should be saved |
public static void serializeFile(String path, Object o)
//package com.java2s; //License from project: Apache License import java.io.FileNotFoundException; import java.io.FileOutputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { /**/*from w ww .j a v a 2 s. c om*/ * Serializes an object and saves it to a file, located at given path. * @param path where the file should be saved * @param o the object, that should be saved */ public static void serializeFile(String path, Object o) { try { JAXBContext context = JAXBContext.newInstance(o.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); FileOutputStream stream = new FileOutputStream(path); m.marshal(o, stream); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }