======serialization demo====================== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package zolt.newprograms; /** * * @author raju_b */ import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "RAJREDDY"; e.address = "HYDERABAD,AP"; e.SSN = 11122333; e.number = 101; e.ram="Hello iam not serialized"; try { FileOutputStream fileOut = new FileOutputStream("emprajreddy"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); }catch(IOException i) { i.printStackTrace(); } } } ======================deserialization demo=========================== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package zolt.newprograms; /** * * @author raju_b */ import java.io.*; import java.util.Calendar; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("emprajreddy"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { System.out.println("Employee class not found."); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); System.out.println("Number: " + e.ram); System.out.println("Flattened time: " + e.timenew); System.out.println("stored time is: " + e.time); System.out.println(); // print out the current time System.out.println("Current time: " + Calendar.getInstance().getTime()); } } =================================Employee class==================== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package zolt.newprograms; import java.io.*; import java.util.Calendar; import java.util.Date; /** * * @author raju_b */ public class Employee implements java.io.Serializable { public String name; public String address; public int SSN; transient public String ram; public int number; public Date time; Date timenew=Calendar.getInstance().getTime(); public Employee() { time = Calendar.getInstance().getTime(); System.out.println("the time in emp is="+time); } public Date getTime() { return time; } public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address); } } ========================Marshalling demo=====JavaToXML==================== import java.math.BigDecimal; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class JavaToXML { public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(Product.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Product object = new Product(); object.setCode("WI1"); object.setName("Widget Number One"); object.setPrice(BigDecimal.valueOf(300.00)); m.marshal(object, System.out); } } ================Un-Marshalling xmltojava===================== import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class XmlToJava { public static void main(String[] args) { try { JAXBContext jc = JAXBContext.newInstance(Product.class); Unmarshaller u = jc.createUnmarshaller(); File f = new File("Product.xml"); Product product = (Product) u.unmarshal(f); System.out.println(product.getCode()); System.out.println(product.getName()); System.out.println(product.getPrice()); } catch (JAXBException e) { e.printStackTrace(); } } }
Tuesday, June 5, 2012
Serialization , Marshalling
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment