序列化
import java.io.*;
class student implements Serializable {
private String name ;
private String id ;
public student(){
}
public student(String name,String id){
this.name=name ;
this.id=id ;
}
public void setName(String name){
this.name=name ;
}
public void setId(String id){
this.id=id ;
}
public String getName(){
return this.name ;
}
public String getId(){
return this.id ;
}
}
public class text1 {
public static void main(String[] args) {
try
{
student st=new student("a001","0001");
File file=new File("student.db");
file.createNewFile();
//序列化
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(st);
oos.close();
fos.close();
//反序列化
FileInputStream fis=new FileInputStream(file);
ObjectInputStream ois=new ObjectInputStream(fis);
student st1=(student)ois.readObject();
ois.close();
fis.close();
System.out.println("name = "+st1.getName());
System.out.println("id = "+st1.getId());
}
catch(Exception e){
e.printStackTrace();
}
}
}
Net序列化
//server
import java.io.*;
import java.net.*;
class student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3426120384019087091L;
private String name ;
private String id ;
public student(){
}
public student(String name,String id){
this.name=name ;
this.id=id ;
}
public void setName(String name){
this.name=name ;
}
public void setId(String id){
this.id=id ;
}
public String getName(){
return this.name ;
}
public String getId(){
return this.id ;
}
}
public class servertest extends ServerSocket{
class ServerThread extends Thread {
private Socket client ;
private student st;
private ObjectOutputStream netout = null ;
public ServerThread(Socket tmp)throws IOException {
client = tmp ;
st=new student("a001","0001");
netout = new ObjectOutputStream(client.getOutputStream());
System.out.println("Client("+client.getInetAddress().getHostAddress()+") connect");
start();
}
public void run(){
try {
netout.writeObject(st);
client.close();
netout.close();
}
catch(IOException e){
e.printStackTrace();
}
}
}
public servertest() throws IOException{
super(8580);
System.out.println("server start:");
for(;;){
Socket socket = accept();
new ServerThread(socket);
}
}
@SuppressWarnings("resource")
public static void main(String[]args){
try {
new servertest();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//client
import java.io.*;
import java.net.*;
public class clienttest {
private student st;
public static void main(String[]args){
Socket client=null ;
ObjectInputStream netin=null ;
try {
client=new Socket("127.0.0.1",8580);
System.out.print("rec [");
netin=new ObjectInputStream(client.getInputStream());
student st=(student)netin.readObject();
netin.close();
client.close();
System.out.println(st.getName()+" - "+st.getId()+"] end");
}
catch(Exception e){
e.printStackTrace();
}
}
}
Transient
import java.io.*;
class student implements Serializable {
private String name ;
private transient String id ;
public student(){
}
public student(String name,String id){
this.name=name ;
this.id=id ;
}
public void setName(String name){
this.name=name ;
}
public void setId(String id){
this.id=id ;
}
public String getName(){
return this.name ;
}
public String getId(){
return this.id ;
}
}
public class text1 {
public static void main(String[] args) {
try
{
student st=new student("a001","0001");
File file=new File("student.db");
file.createNewFile();
//序列化
FileOutputStream fos=new FileOutputStream(file);
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(st);
oos.close();
fos.close();
//反序列化
FileInputStream fis=new FileInputStream(file);
ObjectInputStream ois=new ObjectInputStream(fis);
student st1=(student)ois.readObject();
ois.close();
fis.close();
System.out.println("name = "+st1.getName());
System.out.println("id = "+st1.getId());
}
catch(Exception e){
e.printStackTrace();
}
}
}