private void writeObject(ObjectOutputStream os) throws IOException
{
}
private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException
{
}
package org.arpit.javapostsforlearning;
import java.io.Serializable;
public class Employee implements Serializable{
int employeeId;
String employeeName;
String department;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
package org.arpit.javapostsforlearning;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeMain {
/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
Employee emp = new Employee();
emp.setEmployeeId(101);
emp.setEmployeeName("Arpit");
emp.setDepartment("CS");
try
{
FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
outStream.writeObject(emp);
outStream.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
}
}
package org.arpit.javapostsforlearning;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializeMain {
/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
Employee emp = null;
try
{
FileInputStream fileIn =new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
emp = (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("Emp id: " + emp.getEmployeeId());
System.out.println("Name: " + emp.getEmployeeName());
System.out.println("Department: " + emp.getDepartment());
}
}
Deserialized Employee... Emp id: 101 Name: Arpit Department: CS
package org.arpit.javapostsforlearning;
import java.io.Serializable;
public class Employee implements Serializable{
int employeeId;
String employeeName;
String department;
Address address;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
package org.arpit.javapostsforlearning;
public class Address {
int homeNo;
String street;
String city;
public Address(int homeNo, String street, String city) {
super();
this.homeNo = homeNo;
this.street = street;
this.city = city;
}
public int getHomeNo() {
return homeNo;
}
public void setHomeNo(int homeNo) {
this.homeNo = homeNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package org.arpit.javapostsforlearning;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeDeserializeMain {
/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
Employee emp = new Employee();
emp.setEmployeeId(101);
emp.setEmployeeName("Arpit");
emp.setDepartment("CS");
Address address=new Address(88,"MG road","Pune");
emp.setAddress(address);
//Serialize
try
{
FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
outStream.writeObject(emp);
outStream.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
//Deserialize
emp = null;
try
{
FileInputStream fileIn =new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
emp = (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("Emp id: " + emp.getEmployeeId());
System.out.println("Name: " + emp.getEmployeeName());
System.out.println("Department: " + emp.getDepartment());
address=emp.getAddress();
System.out.println("City :"+address.getCity());
}
}
java.io.NotSerializableException: org.arpit.javapostsforlearning.Address at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) at java.io.ObjectOutputStream.writeSerialData(Unknown Source) at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source)
import java.io.Serializable;
public class Address implements Serializable{
int homeNo;
String street;
String city;
public Address(int homeNo, String street, String city) {
super();
this.homeNo = homeNo;
this.street = street;
this.city = city;
}
public int getHomeNo() {
return homeNo;
}
public void setHomeNo(int homeNo) {
this.homeNo = homeNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
Deserialized Employee... Emp id: 101 Name: Arpit Department: CS City :Pune
package org.arpit.javapostsforlearning;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Employee implements Serializable{
int employeeId;
String employeeName;
String department;
transient Address address;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
private void writeObject(ObjectOutputStream os) throws IOException, ClassNotFoundException
{
try {
os.defaultWriteObject();
os.writeInt(address.getHomeNo());
os.writeObject(address.getStreet());
os.writeObject(address.getCity());
}
catch (Exception e)
{ e.printStackTrace(); }
}
private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException
{
try {
is.defaultReadObject();
int homeNo=is.readInt();
String street=(String) is.readObject();
String city=(String) is.readObject();
address=new Address(homeNo,street,city);
} catch (Exception e) { e.printStackTrace(); }
}
}
package org.arpit.javapostsforlearning;
import java.io.Serializable;
public class Address {
int homeNo;
String street;
String city;
public Address(int homeNo, String street, String city) {
super();
this.homeNo = homeNo;
this.street = street;
this.city = city;
}
public int getHomeNo() {
return homeNo;
}
public void setHomeNo(int homeNo) {
this.homeNo = homeNo;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
package org.arpit.javapostsforlearning;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeDeserializeMain {
/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
Employee emp = new Employee();
emp.setEmployeeId(101);
emp.setEmployeeName("Arpit");
emp.setDepartment("CS");
Address address=new Address(88,"MG road","Pune");
emp.setAddress(address);
//Serialize
try
{
FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
outStream.writeObject(emp);
outStream.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
//Deserialize
emp = null;
try
{
FileInputStream fileIn =new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
emp = (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("Emp id: " + emp.getEmployeeId());
System.out.println("Name: " + emp.getEmployeeName());
System.out.println("Department: " + emp.getDepartment());
address=emp.getAddress();
System.out.println("City :"+address.getCity());
}
}
Deserialized Employee... Emp id: 101 Name: Arpit Department: CS City :Pune
package org.arpit.javapostsforlearning;
public class Person {
String name="default";
String nationality;
public Person()
{
System.out.println("Person:Constructor");
}
public Person(String name, String nationality) {
super();
this.name = name;
this.nationality = nationality;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
}
package org.arpit.javapostsforlearning;
import java.io.Serializable;
public class Employee extends Person implements Serializable{
int employeeId;
String department;
public Employee(int employeeId,String name,String department,String nationality)
{
super(name,nationality);
this.employeeId=employeeId;
this.department=department;
System.out.println("Employee:Constructor");
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
package org.arpit.javapostsforlearning;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeDeserializeMain {
/**
* @author Arpit Mandliya
*/
public static void main(String[] args) {
//Serialize
Employee emp = new Employee(101,"Arpit","CS","Indian");
System.out.println("Before serializing");
System.out.println("Emp id: " + emp.getEmployeeId());
System.out.println("Name: " + emp.getName());
System.out.println("Department: " + emp.getDepartment());
System.out.println("Nationality: " + emp.getNationality());
System.out.println("************");
System.out.println("Serializing");
try
{
FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
outStream.writeObject(emp);
outStream.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
//Deserialize
System.out.println("************");
System.out.println("Deserializing");
emp = null;
try
{
FileInputStream fileIn =new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
emp = (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("After serializing");
System.out.println("Emp id: " + emp.getEmployeeId());
System.out.println("Name: " + emp.getName());
System.out.println("Department: " + emp.getDepartment());
System.out.println("Nationality: " + emp.getNationality());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有