Java实验课6

本文介绍了如何使用Java的序列化机制,通过SerialandDeserial类实现User对象的隐私保护,仅存储用户ID而不暴露姓名。通过实例演示了如何创建、复制User对象并存储到文件中,确保数据安全传输。
摘要由CSDN通过智能技术生成

Java实验课6——串行化对象输出

为了将Java对象输出存储到外存中,Java提供对象输出流。

例7.14

实现用户的复制,但要对用户姓名进行隐私保护。

设计步骤:
①定义User类。
类:User
字段:userID:int
userName:transient String
方法:User(int,String)
toString()
②定义SerialandDeserial类。
类:SerialandDeserial
字段:ferry:File
方法:SerialandDeserial(String)
void execSerialize(User us) 串行化
User execDeserialize() 反串行化
③在main()中创建user对象作为源对象,变量us2接收目标;输出us,用于与目标us2对比;定义SerialandDeserial对象ferryBoat,用于复制;调用ferryBoat.execSerialand(User)和ferryBoat.execDeserial()进行对象复制;输出us2进行对比。
代码如下:

 import java.io.*;
public class Jpro1_1{
	static class User implements Serializable{
		private int userID;
		private transient String userName;
		public User(int id,String name){
			userID=id;
			userName=name;  //串行时不会传送具体的值
		}
		public String tostring(){
			return userID+":"+userName;
		}
	}
	static class SerialandDeserial{
		//串行化的目标文件、反串行化的源文件。
		File ferry=null;
		public SerialandDeserial(String str){
			ferry=new File(str);
		}
		public void execSerialize(User us)throws IOException{
			//串行化
			FileOutputStream fout=new FileOutputStream(ferry);
			ObjectOutputStream ob=new ObjectOutputStream(fout);
			ob.writeObject(us);
			ob.close();
		}
		public User execDeserialize() throws IOException,
		                          ClassNotFoundException{
		    //反串行化
		    FileInputStream fis=new FileInputStream(ferry);
		    ObjectInputStream oi=new ObjectInputStream(fis);
		    User us=(User)oi.readObject();
		    oi.close();
		    return us;
		}
	}
	public static void main(String[] args)throws IOException, ClassNotFoundException{
		User us=new User(17, "ljx");
		User us2;
		System.out.println("转换之前:"+us);
		SerialandDeserial ferryBoat=new SerialandDeserial("data.dat");
		//经查询,data.dat是电脑中应用程序数据的意思
		ferryBoat.execSerialize(us);
		us2=ferryBoat.execDeserialize();
		System.out.println("转换之后:"+us2);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a class called Person with the following attributes: title (Mr., Mrs., Ms., etc.) first name last name nickname age in years sex (boolean - true/false to indicated either male or female) Write a constructor that takes no parameters and performs no initializations. Write a constructor that takes a parameter for each of the attributes listed above and sets them within the objects by calling the setter methods listed below. The Person class should have a setter method and a getter method with public access for each attribute. In the setter methods, get rid of any leading or trailing spaces (String trim() method). For a Person with the following attributes: title = "Mr." first name = "Michael" last name = "Zheng" nickname = "Mike" age = 22 sex = true (true is male, false is female) The Person class should have the following public access methods that return Strings as follows: standardName() concatenation of the first and last names (i.e., "Michael Zheng") formalName() concatenation of the title, first name, lastname (i.e., "Mr. Michael Zheng") casualName() return the nickname if it is not null, otherwise return the first name (i.e., "Mike") Be realistic when generating names. If a particular attribute does not exist for a given person, don't try to concatenate it. If necessary, add appropriate spacing and punctuation, but do not leave any leading or trailing spaces in the String that is returned. MakePerson Write a class called MakePerson with a main() method that instantiates 2 Person objects. Initialize the attributes of one of the Person objects by supplying parameters to it's constructor. Instantiate the other Person object with the default constructor (that does not accept any parameters), then set it's attributes via the appropriate setter methods. For each of the Person objects, execute and print (System.out.println()) the results of all of the getter methods and of the standardName(), formalName(), and casualName() methods.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值