无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

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

java對(duì)象流

2022-08-25 12:33 作者:虛云幻仙  | 我要投稿

/**
* 對(duì)象流ObjectInputStream/ObjectOutputStream
* 處理流,將對(duì)象序列化(類似編碼)通過節(jié)點(diǎn)流存儲(chǔ)、反序列化(類似解碼)通過節(jié)點(diǎn)流讀取對(duì)象。常用于將對(duì)象保存至文件、網(wǎng)絡(luò)傳輸及從文件、網(wǎng)絡(luò)中讀取對(duì)象
* 序列化/串行化serialization對(duì)象時(shí)需要對(duì)象實(shí)現(xiàn)Serializable可串行化的/可序列化的 接口
*/

public class ObjectStream1 {
? ?public static void main(String[] args) {
? ? ? ?try (ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("iostream/testObject.objects")))){
? ? ? ? ? ?//字節(jié)節(jié)點(diǎn)流一般會(huì)外套緩沖流,因?yàn)樽止?jié)流讀取文件是逐字節(jié)進(jìn)行讀取的,所以后綴是否有和后綴寫什么都不會(huì)影響文件的讀取,而因?yàn)槲募袑懭氲氖菍?duì)象,所以一般常用的各種類型的軟件都不可能正確讀取內(nèi)容,也就不需要設(shè)定為txt了
? ? ? ? ? ?oos.writeBoolean(true);
? ? ? ? ? ?oos.writeByte(255);
? ? ? ? ? ?oos.writeChar('a');
? ? ? ? ? ?oos.writeDouble(Math.random());
? ? ? ? ? ?oos.writeUTF("對(duì)象流也可以存取基礎(chǔ)數(shù)據(jù)類型,用法和數(shù)據(jù)流相同,但兩者不是繼承關(guān)系");
? ? ? ? ? ?oos.flush();
? ? ? ? ? ?//用記事本打開結(jié)果為: wq a?鈹墫圔? c瀵硅薄嫻佷篃鍙互瀛樺彇鍩虹鏁版嵁綾誨瀷錛岀敤娉曞拰鏁版嵁嫻佺浉鍚岋紝浣嗕袱鑰呬笉鏄戶鎵垮叧緋?
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?try(ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("iostream/testObject.objects")))){
? ? ? ? ? ?boolean bo = ois.readBoolean();
? ? ? ? ? ?byte by = ois.readByte();
? ? ? ? ? ?char ch = ois.readChar();
? ? ? ? ? ?double dou = ois.readDouble();
? ? ? ? ? ?String st = ois.readUTF();
? ? ? ? ? ?System.out.println(bo+"\t"+by+"\t"+ch+"\t"+dou+"\t"+st);
? ? ? ? ? ?//和數(shù)據(jù)流相同,必須按照存儲(chǔ)順序進(jìn)行數(shù)據(jù)類型的讀取,直接返回原數(shù)據(jù)類型,結(jié)果為:true ?-1 a ?0.50018980655707
? ? ? ? ? ?// 對(duì)象流也可以存取基礎(chǔ)數(shù)據(jù)類型,用法和數(shù)據(jù)流相同,但兩者不是繼承關(guān)系,對(duì)象流在實(shí)例化時(shí)會(huì)實(shí)例化一個(gè)數(shù)據(jù)流對(duì)象,一部分基礎(chǔ)數(shù)據(jù)類型的存取方法是通過數(shù)據(jù)流對(duì)象完成的

? ? ? ?} catch (FileNotFoundException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ?}
}

class UserObject implements Serializable{
? ?//創(chuàng)建可序列化對(duì)象,需要實(shí)現(xiàn)Serializable接口,Serializable接口內(nèi)沒有任何方法,是一個(gè)標(biāo)記接口,想要序列化必須打標(biāo)記
? ?//創(chuàng)建javabean

? ?private int id;
? ?private String name;
? ?private boolean gender;
? ?//gender性別

? ?public UserObject(int id, String name, boolean gender) {
? ? ? ?this.id = id;
? ? ? ?this.name = name;
? ? ? ?this.gender = gender;
? ?}

? ?public UserObject() {
? ?}

? ?public int getId() {
? ? ? ?return id;
? ?}

? ?public void setId(int id) {
? ? ? ?this.id = id;
? ?}

? ?public String getName() {
? ? ? ?return name;
? ?}

? ?public void setName(String name) {
? ? ? ?this.name = name;
? ?}

? ?public boolean isGender() {
? ? ? ?return gender;
? ?}

? ?public void setGender(boolean gender) {
? ? ? ?this.gender = gender;
? ?}

? ?@Override
? ?public String toString() {
? ? ? ?return "UserObject{" +
? ? ? ? ? ? ? ?"id=" + id +
? ? ? ? ? ? ? ?", name='" + name + '\'' +
? ? ? ? ? ? ? ?", gender=" + gender +
? ? ? ? ? ? ? ?'}';
? ?}
}

class ObjectStream2{
? ?public static void main(String[] args) {
? ? ? ?UserObject user1 = new UserObject(1,"zhao",true);
? ? ? ?UserObject user2 = new UserObject(2,"sun",false);
? ? ? ?UserObject user3 = new UserObject(3,"li",true);

? ? ? ?try(ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("iostream/testObject.objects")))) {
? ? ? ? ? ?oos.writeObject(user1);
? ? ? ? ? ?oos.writeObject(user2);
? ? ? ? ? ?oos.writeObject(user3);
? ? ? ? ? ?//序列化
? ? ? ? ? ?oos.flush();
? ? ? ?} catch (FileNotFoundException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}

? ? ? ?try(ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("iostream/testObject.objects")))) {
? ? ? ? ? ?UserObject u1 = (UserObject) ois.readObject();
? ? ? ? ? ?//反序列化.readObject()返回Object對(duì)象,需要強(qiáng)轉(zhuǎn)回User,這意味著讀取方需要知道對(duì)象的原始類型并且讀取方JVM虛擬機(jī)能找到這個(gè)類(否則拋ClassNotFoundException)
? ? ? ? ? ?UserObject u2 = (UserObject) ois.readObject();
? ? ? ? ? ?UserObject u3 = (UserObject) ois.readObject();
? ? ? ? ? ?System.out.println(u1);
? ? ? ? ? ?System.out.println(u2);
? ? ? ? ? ?System.out.println(u3);
? ? ? ? ? ?//結(jié)果為UserObject{id=1, name='zhao', gender=true} UserObject{id=2, name='sun', gender=false} UserObject{id=3, name='li', gender=true}
? ? ? ?} catch (FileNotFoundException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?} catch (ClassNotFoundException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}
? ?}
}

java對(duì)象流的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
阿合奇县| 临高县| 嘉义县| 元谋县| 雅安市| 琼结县| 寿宁县| 平利县| 芒康县| 渭源县| 浙江省| 海门市| 修文县| 临高县| 岢岚县| 滕州市| 龙里县| 广东省| 汝阳县| 厦门市| 西乌珠穆沁旗| 始兴县| 霸州市| 广灵县| 桃源县| 焦作市| 珠海市| 宽城| 将乐县| 南皮县| 慈溪市| 彩票| 天等县| 白城市| 新闻| 会东县| 鹤岗市| 南郑县| 清流县| 内黄县| 和林格尔县|