[十二]java作业

本文介绍了Java中序列化的三种应用场景:文件存储、网络传输及转瞬属性的忽略,并提供了详细的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

序列化

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();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值