五月天青色头像情侣网名,国产亚洲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();
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?
?? ?}
}


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

分享到微博請遵守國家法律
定陶县| 岱山县| 鹤山市| 建湖县| 合江县| 长岛县| 丰都县| 镇平县| 扬州市| 义马市| 衢州市| 禹州市| 辽宁省| 永泰县| 吉安县| 洪江市| 肇源县| 珲春市| 温州市| 永定县| 明溪县| 顺昌县| 瑞昌市| 长泰县| 安义县| 普安县| 东乡县| 沙洋县| 石家庄市| 道孚县| 旌德县| 吉林市| 遂昌县| 承德市| 横峰县| 顺昌县| 浦东新区| 邯郸县| 象山县| 保德县| 海盐县|