transient:該數(shù)據(jù)不需要序列化,數(shù)據(jù)透明(輸出顯示null),private transient String name;
創(chuàng)新互聯(lián)主營曾都網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app軟件開發(fā)公司,曾都h5小程序制作搭建,曾都網(wǎng)站營銷推廣歡迎曾都等地區(qū)企業(yè)咨詢
//寫出后讀取,讀取的數(shù)徐與寫出保持一致
public class n {
public static void main(String[]args) throws IOException, ClassNotFoundException
{
寫入字節(jié)數(shù)組
//寫出 序列化
ByteArrayOutputStream os=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(os));
ha ff=new ha("hh",222);
oos.writeUTF("哈哈");
oos.writeChar('q');
oos.writeBoolean(false);
oos.writeObject(ff);
oos.flush();
byte[] datas=os.toByteArray();
oos.close();
//讀取 反序列化
ObjectInputStream ois =new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
String s=ois.readUTF();
char ch=ois.readChar();
boolean flag=ois.readBoolean();
Object str=ois.readObject(); //Object需要強(qiáng)制轉(zhuǎn)換
//還原(判斷是哪個類,然后強(qiáng)制轉(zhuǎn)換):
if (str instanceof String ) //是String類就直接轉(zhuǎn)換成字符串
{
String s1=(String)str;
System.out.println(s1);
}
if(str instanceof Data)
{
Date d=(Date)str;
System.out.println(d);
}
if(str instanceof ha)
{
ha fr=(ha)str;
System.out.println(fr.getName()+fr.getSalary());
}
ois.close();
}
//javabean,用于封裝數(shù)據(jù)
class ha implements java.io.Serializable {
private transient String name; //該數(shù)據(jù)不需要序列化,數(shù)據(jù)透明
private double salary;
public ha()
{
}
public ha(String name,double salary)
{
this.name=name;
this.salary=salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
寫入文件:
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("D:/d/s")));
ha ff=new ha("hh",222);
oos.writeUTF("what");
oos.writeObject(ff);
oos.flush();
oos.close();
//使用時,反序列化對象
ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("D:/d/s")));
String s=ois.readUTF();
Object it=ois.readObject();
if(it instanceof ha)
{
ha q=(ha)it;
System.out.println(q.getName()+q.getSalary());
}
ois.close();
//javabean 用于封裝數(shù)據(jù)
class ha implements java.io.Serializable {
private transient String name; //該數(shù)據(jù)不需要序列化,數(shù)據(jù)透明
private double salary;
public ha()
{
}
public ha(String name,double salary)
{
this.name=name;
this.salary=salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}