Here you can find the source of saveDataToFile(File file, T data)
Parameter | Description |
---|---|
file | Points to a valid xml file containing data that match the classToConvert . Cannot be null. |
Parameter | Description |
---|---|
FileNotFoundException | Thrown if the file is missing. |
JAXBException | Thrown if there is an error during converting the datainto xml and writing to the file. |
public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileNotFoundException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; public class Main { /**// w w w . j a v a 2s. c o m * Saves the data in the file in xml format. * * @param file Points to a valid xml file containing data that match the {@code classToConvert}. * Cannot be null. * @throws FileNotFoundException Thrown if the file is missing. * @throws JAXBException Thrown if there is an error during converting the data * into xml and writing to the file. */ public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException { assert file != null; assert data != null; if (!file.exists()) { throw new FileNotFoundException("File not found : " + file.getAbsolutePath()); } JAXBContext context = JAXBContext.newInstance(data.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(data, file); } }