五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

對象流寫出到文件的代碼

2020-04-04 12:52 作者:小垃圾kiki  | 我要投稿
package cn.jd.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/*
?* 對象流:
?* 1先寫出后讀取
?* 2讀取的順序和寫出保持一致
?* 3不是所有的對象都可以序列化Serializable
?*/
public class ObjectTest3 {
?? ?public static void main(String[] args) throws IOException, ClassNotFoundException {
?? ??? ?
?? ??? ?ObjectOutputStream oos=
?? ??? ??? ??? ?new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj.ser")));
?? ??? ?oos.writeUTF("我太難了");
?? ??? ?oos.writeInt(18);
?? ??? ?oos.writeBoolean(false);
?? ??? ?//加入對象
?? ??? ?oos.writeObject("希望世界和平");
?? ??? ?oos.writeObject(new Date());
?? ??? ?Employee emp=new Employee("小二",400);
?? ??? ?oos.writeObject(emp);
?? ??? ?oos.flush();
?? ??? ?oos.close();
?? ??? ? ?
?? ??? ?ObjectInputStream ois=
?? ??? ??? ??? ?new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj.ser")));
?? ??? ? String msg=ois.readUTF();
?? ??? ? int age=ois.readInt();
?? ??? ? boolean flag=ois.readBoolean();
?? ??? ? Object str=ois.readObject();
?? ??? ? Object date=ois.readObject();
?? ??? ? Object employee=ois.readObject();
?? ??? ? //接下來我們就將類型還原,這里我們必須加上類型轉(zhuǎn)換
?? ??? ? if(str instanceof String) {
?? ??? ??? ? String strObj=(String)str;
//?? ??? ??? ? System.out.println(strObj);
?? ??? ? }
?? ??? ? if(date instanceof Date) {
?? ??? ??? ? Date dateObj=(Date)date;
//?? ??? ??? ? System.out.println(dateObj);
?? ??? ? }
?? ??? ? if(employee instanceof Employee) {
?? ??? ??? ?Employee empObj=(Employee)employee;
?? ??? ??? ? System.out.println(empObj.getName()+"-->"+empObj.getSalary());
?? ??? ? }
//?? ??? ? System.out.println(msg);
?? ??? ? ois.close();
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?
?? ?}
}


對象流寫出到文件的代碼的評論 (共 條)

分享到微博請遵守國家法律
连江县| 包头市| 拜泉县| 华池县| 镇雄县| 会同县| 雷山县| 石狮市| 深圳市| 宝清县| 竹山县| 松溪县| 稻城县| 鄄城县| 平安县| 云南省| 德庆县| 新巴尔虎右旗| 五河县| 石景山区| 利川市| 兖州市| 石河子市| 明星| 甘南县| 浙江省| 明水县| 昌邑市| 徐闻县| 肇源县| 菏泽市| 桐柏县| 巧家县| 密山市| 黔西县| 增城市| 手机| 文成县| 桃园县| 新兴县| 高阳县|