Here you can find the source of deserializeFile(String path, Class
Parameter | Description |
---|---|
path | where the file is located |
clazz | type of the resulting object |
public static <T> T deserializeFile(String path, Class<T> clazz)
//package com.java2s; //License from project: Apache License import java.io.FileInputStream; import java.io.FileNotFoundException; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class Main { /**/*from w w w.j a v a 2 s . co m*/ * Reads a file and deserializes it to an object of given class. * @param path where the file is located * @param clazz type of the resulting object * @return an object of given class */ public static <T> T deserializeFile(String path, Class<T> clazz) { try { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller m = context.createUnmarshaller(); Object o = m.unmarshal(new FileInputStream(path)); return clazz.cast(o); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } }