transient关键字的学习

在开发的过程中基本没有用过transient这个关键字来修饰变量,就学习一下:
参考文档:https://www.cnblogs.com/tiantanglw/p/9142895.html

直接上代码了

package com;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class testTransient implements Serializable{
/**
	 * 
	 */
	private static final long serialVersionUID = 5622216320102861713L;
private String name="test";
private int age=10;
private transient String passwd="123";

@Override
public String toString() {
	return "testTransient [name=" + name + ", age=" + age + ", passwd="
			+ passwd + "]";
}
public static void main(String[] args) {
	testTransient t=new testTransient();
	System.out.println("写入前:"+t.toString());
	FileOutputStream fi=null;
	ObjectOutputStream o=null;
	try {
		 fi=new FileOutputStream("D://transient.txt");
	     o=new ObjectOutputStream(fi);
	     o.writeObject(t);
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}catch (IOException e) {
		e.printStackTrace();
	}finally{
		if(fi!=null){
			try {
				fi.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(o!=null){
			try {
				o.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	FileInputStream fis=null;
	ObjectInputStream os=null;
	try {
		 fis=new FileInputStream("D://transient.txt");
	     os=new ObjectInputStream(fis);
	     testTransient t1;
			t1 = (testTransient) os.readObject();
	     System.out.println("写入后:"+t1.toString());
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}catch (IOException e) {
		e.printStackTrace();
	}catch (ClassNotFoundException e) {
		e.printStackTrace();
	}finally{
		if(fis!=null){
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(os!=null){
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
}

控制台打印内容:

写入前:testTransient [name=test, age=10, passwd=123]
写入后:testTransient [name=test, age=10, passwd=null]

实现 Externalizable 接口,据说也可以实现transient的效果,但是我给变量passwd加上和不加的输出结果是一样的。

package com;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
public class testTransient implements Externalizable{

private String name="test";
private int age=10;
private  transient String passwd="123";


public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getPasswd() {
	return passwd;
}
public void setPasswd(String passwd) {
	this.passwd = passwd;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
	out.writeObject(name);
	out.writeObject(age);
//	out.writeObject(passwd);
@Override
public void readExternal(ObjectInput in) throws IOException,
		ClassNotFoundException {
	this.name=(String) in.readObject();
	this.age=(Integer) in.readObject();
//	this.passwd=(String) in.readObject();
}

@Override
public String toString() {
	return "testTransient [name=" + name + ", age=" + age + ", passwd="
			+ passwd + "]";
}
public static void main(String[] args) {
	testTransient t=new testTransient();
	t.setAge(18);
	t.setName("tran");
	t.setPasswd("123");
	System.out.println("写入前:"+t.toString());
	FileOutputStream fi=null;
	ObjectOutputStream o=null;
	try {
		 fi=new FileOutputStream("D://transient2.txt");
	     o=new ObjectOutputStream(fi);
	     o.writeObject(t);
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}catch (IOException e) {
		e.printStackTrace();
	}finally{
		if(fi!=null){
			try {
				fi.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(o!=null){
			try {
				o.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	FileInputStream fis=null;
	ObjectInputStream os=null;
	try {
		 fis=new FileInputStream("D://transient2.txt");
	     os=new ObjectInputStream(fis);
	     testTransient t1 = (testTransient) os.readObject();
	     System.out.println("写入后:"+t1.toString());
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}catch (IOException e) {
		e.printStackTrace();
	}catch (ClassNotFoundException e) {
		e.printStackTrace();
	}finally{
		if(fis!=null){
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if(os!=null){
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

}

输出结果:

写入前:testTransient [name=tran, age=18, passwd=123]
写入后:testTransient [name=tran, age=18, passwd=123]

不知道为什么,所以通过这个demo大概知道了transient的用法。

发布了81 篇原创文章 · 获赞 10 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/fhf2424045058/article/details/101060635