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

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

對象流的源碼

2020-04-04 12:34 作者:小垃圾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.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/*
?* 對象流:
?* 1先寫出后讀取
?* 2讀取的順序和寫出保持一致
?* 3不是所有的對象都可以序列化Serializable
?*/
public class ObjectTest2 {
?? ?public static void main(String[] args) throws IOException, ClassNotFoundException {
?? ??? ?ByteArrayOutputStream? baos=new ByteArrayOutputStream();
?? ??? ?ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(baos));
?? ??? ?oos.writeUTF("我太難了");
?? ??? ?oos.writeInt(18);
?? ??? ?oos.writeBoolean(false);
?? ??? ?//加入對象
?? ??? ?oos.writeObject("希望世界和平");
?? ??? ?oos.writeObject(new Date());
?? ??? ?Employee emp=new Employee("小二",400);
?? ??? ?oos.writeObject(emp);
?? ??? ?oos.flush();
?? ??? ?byte[]? datas=baos.toByteArray();? ?
?? ??? ?ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
?? ??? ? 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);
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?
?? ?}
}
//寫了一個javabean類?? 后期用來封裝數(shù)據(jù)?? transient
class Employee? implements java.io.Serializable{
?? ?private transient String name;
?? ?private double? salary;
?? ?public Employee() {
?? ??? ?
?? ?}
?? ?public Employee(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;
?? ?}
?? ?
?? ?
}


對象流的源碼的評論 (共 條)

分享到微博請遵守國家法律
禄劝| 内丘县| 石台县| 陇西县| 平江县| 察雅县| 历史| 汤原县| 金塔县| 万山特区| 大埔县| 宜良县| 阿城市| 若尔盖县| 门源| 合肥市| 葵青区| 平阴县| 远安县| 新丰县| 和顺县| 景东| 驻马店市| 厦门市| 衡阳县| 山丹县| 昭觉县| 天门市| 民和| 白沙| 民乐县| 益阳市| 临武县| 营口市| 崇左市| 韶关市| 关岭| 神池县| 阳春市| 涞源县| 昭苏县|