• java:序列化Serializable 接口


    java:序列化Serializable 接口

    public class SerializePerson implements Serializable {
    
    	private String name;
    	private int age;
    	
    	public SerializePerson(String name, int age)
    	{
    		
    		this.name = name;
    		this.age = age;
    	}
    
    	@Override
    	public String toString() {
    		return "姓名:" + name + ", 年龄:" + age;
    	}
    	
    	
    	
    	
    }
    

      

    一,单对象序列化

    public static void main(String[] args) throws Exception, Exception {
    		// TODO 自动生成的方法存根
    		
    		
    		
    		if(  args[0].equals("set") )
    		{
    			
    			setPseron();
    			
    		}else if( args[0].equals("get") )
    		{
    			getPseron();
    		}else{
    			System.out.println("抱歉,你什么都没有输入");
    		}
    		System.out.println(args[0]);
    
    	}
    	
    	public static void setPseron() throws Exception, IOException
    	{
    		
    		File file = new File("F:"+File.separator+"work"+File.separator+"work"+File.separator+"a.txt");
    		ObjectOutputStream oobs = null;
    		oobs = new ObjectOutputStream( new FileOutputStream(file) );
    		oobs.writeObject(new SerializePerson("张三",22));
    		oobs.close();
    		
    		
    	}
    	
    	public static void getPseron() throws Exception, IOException
    	{
    		File file = new File("F:"+File.separator+"work"+File.separator+"work"+File.separator+"a.txt");
    		ObjectInputStream oips = null;
    		oips = new ObjectInputStream( new FileInputStream(file) );
    		Object obj = oips.readObject();
    		SerializePerson per = (SerializePerson) obj;
    		System.out.println(per);
    		 
    	}
    

      

    二。多对象,多数组序列化

    public static void main(String[] args) throws Exception, Exception
    	{
    		
    		
    		if(args[0].equals("set"))
    		{
    			
    			setPerson();
    		}else if(args[0].equals("get"))
    		{
    			Object obj = getPerson();
    			SerializePerson per[] = (SerializePerson[]) obj;
    			print(per);
    			
    		}else{
    			System.out.println("请输入一个操作");
    		}
    		
    	}
    	
    	public static void setPerson() throws Exception, IOException
    	{
    		File file = new File("F:"+File.separator+"work"+File.separator+"work"+File.separator+"person.per");
    		ObjectOutputStream  oopt = new ObjectOutputStream( new FileOutputStream(file) );
    		
    		SerializePerson per[] = {new SerializePerson("张三",22), new SerializePerson("李四",44), new SerializePerson("王五",33)};
    		oopt.writeObject(per);		
    		oopt.close();
    	}
    	
    	public static Object getPerson() throws Exception, IOException
    	{
    		File file = new File("F:"+File.separator+"work"+File.separator+"work"+File.separator+"person.per");
    		ObjectInputStream lis = null;
    		lis = new ObjectInputStream( new FileInputStream(file) );
    		Object obj = null;
    		obj = lis.readObject();
    		lis.close();
    		return obj;
    		
    	}
    	
    	public static void print(SerializePerson per[])
    	{
    		for(SerializePerson p: per)
    		{
    			System.out.println(p);
    		}
    	}
    

      

  • 相关阅读:
    SOGo 2.0 发布,群组协同工作系统
    微软随.NET 4.5发布新REST API框架
    DynamicReports 3.0.2 发布,Java 报表方案
    使用 Ant 集成 IBM Security AppScan Standard 进行自动测试
    SUSE 用 zypper 工具 安装 rpm
    嵌入式ARM系统中OpenCV的移植
    qtopiax86安装配置及编程方法
    [转]QTCreator的使用
    在Qt Creator中使用OpenCV库
    vim
  • 原文地址:https://www.cnblogs.com/achengmu/p/7368092.html
Copyright © 2020-2023  润新知