Java XML JAXB Serialize serializeFile(String path, Object o)

Here you can find the source of serializeFile(String path, Object o)

Description

Serializes an object and saves it to a file, located at given path.

License

Apache License

Parameter

Parameter Description
path where the file should be saved
o the object, that should be saved

Declaration

public static void serializeFile(String path, Object o) 

Method Source Code

//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();
        }
    }
}

Related

  1. serialize(Object obj)
  2. serialize(Object obj)
  3. serialize(Object object, Class clazz, String filename)
  4. serialize(T object, Class objectClass, OutputStream resultStream)
  5. serialize(T object, Path path)
  6. serializeToFile(Class clazz, T object, String outputFile)
  7. serializeToSTD(Object obj)
  8. serializeToString(Object obj)
  9. serializeToString(Object obj)