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

歡迎光臨散文網(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;
?? ?}
?? ?
?? ?
}


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

分享到微博請遵守國家法律
深水埗区| 德兴市| 鸡西市| 浙江省| 旌德县| 惠东县| 阜南县| 休宁县| 曲阳县| 湘西| 闽侯县| 珲春市| 高安市| 灵石县| 晋城| 易门县| 和平县| 龙门县| 永济市| 红安县| 万全县| 上犹县| 泾阳县| 湟中县| 叶城县| 庆云县| 甘南县| 德阳市| 漯河市| 大石桥市| 贵德县| 永昌县| 彩票| 蒲江县| 景宁| 泸水县| 库车县| 宁晋县| 太仆寺旗| 阿勒泰市| 静乐县|