zoukankan      html  css  js  c++  java
  • Java关键字——transient

    当使用Serializable接口实现序列化操作时,如果一个对象中的某一属性不希望被序列化,则可以使用transient关键字进行声明

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    
    //=================================================
    // File Name       :	Serializable_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:Person_3
    //属性:
    //方法:
    class Person_3 implements Serializable{		//此类的对象可以被序列化
    	private transient String name;
    	private int age;
    	
    	public Person_3(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	@Override
    	public String toString() {
    		return "姓名:" + name + ", 年龄:" + age;
    	}
    	
    	
    }
    
    
    
    //主类
    //Function        : 	Serializable_demo;
    public class Serializable_demo {
    
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    //		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    //		ObjectOutputStream oos = null;
    //		OutputStream out = new FileOutputStream(f);		//文件输出流
    //		oos = new ObjectOutputStream(out);						//为对象输出流实例化
    //		oos.writeObject(new Person_3("张三", 30));
    //		oos.close();
    		
    		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    		ObjectInputStream ois = null;
    		InputStream input = new FileInputStream(f);		//文件输入流
    		ois = new ObjectInputStream(input);						//为对象输入流实例化
    		Object obj = ois.readObject();									//读取对象
    		ois.close();
    		System.out.println(obj);
    	}
    
    }
    

    <3>序列化一组对象

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    
    //=================================================
    // File Name       :	Serializable_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:Person_3
    //属性:
    //方法:
    class Person_3 implements Serializable{		//此类的对象可以被序列化
    //	private transient String name;
    	private String name;
    	private int age;
    	
    	public Person_3(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	@Override
    	public String toString() {
    		return "姓名:" + name + ", 年龄:" + age;
    	}
    	
    	
    }
    
    
    
    //主类
    //Function        : 	Serializable_demo;
    public class Serializable_demo {
    
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    //		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    //		ObjectOutputStream oos = null;
    //		OutputStream out = new FileOutputStream(f);		//文件输出流
    //		oos = new ObjectOutputStream(out);						//为对象输出流实例化
    //		oos.writeObject(new Person_3("张三", 30));
    //		oos.close();
    		
    //		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    //		ObjectInputStream ois = null;
    //		InputStream input = new FileInputStream(f);		//文件输入流
    //		ois = new ObjectInputStream(input);						//为对象输入流实例化
    //		Object obj = ois.readObject();									//读取对象
    //		ois.close();
    //		System.out.println(obj);
    		
    		Person_3 per[] = {new Person_3("张三",30),new Person_3("李四",31),new Person_3("王五",32)};//定义对象数组
    		ser(per);					//序列化对象数组
    		Object o[] = dser();
    		for(int i=0;i<o.length;i++){
    			Person_3 p = (Person_3) o[i];
    			System.out.println(p);
    		}
    	}
    	
    	public static void ser(Object obj[]) throws Exception{
    		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    		ObjectOutputStream oos = null;
    		OutputStream out = new FileOutputStream(f);		//文件输出流
    		oos = new ObjectOutputStream(out);						//为对象输出流实例化
    		oos.writeObject(obj);
    		oos.close();
    	}
    	
    	public static Object[] dser() throws Exception{
    		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    		ObjectInputStream ois = null;
    		InputStream input = new FileInputStream(f);		//文件输入流
    		ois = new ObjectInputStream(input);						//为对象输入流实例化
    		Object obj[] = (Object[])ois.readObject();				//读取对象数组
    		ois.close();
    		return obj;
    	}
    
    }
    
  • 相关阅读:
    Effective C++笔记:继承与面向对象设计
    华为手机连不上adb解决方法
    android手机出现sqlite3 not found的解决方法
    adb permission denied
    Apache2.4为什么启动报错Cannot load php5apache2_4.dll into server
    界面排版-TableLayout的stretchColumns方法
    java中静态代码块的用法 static用法详解(转)
    Activity not started, its current task has been brought to the front
    (转)在低版本的SDK里使用高版本函数@SuppressLint("NewApi") or @TargetApi?
    Android笔记:解决 Your project contains error(s),please fix them before running your application问题
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5282391.html
Copyright © 2011-2022 走看看