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需要強制轉(zhuǎn)換
//還原(判斷是哪個類,然后強制轉(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;
}
}
文章名稱:java_io_對象流,能夠保存基本數(shù)據(jù)類型和對象
鏈接URL:http://jinyejixie.com/article40/ijccho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、定制網(wǎng)站、響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站、服務(wù)器托管、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)