更新時(shí)間:2020-11-20 17:49:50 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1213次
我們在程序設(shè)計(jì)的過程中,都是用類和對象來描述定義,能不能直接把對象進(jìn)行傳輸呢?答案當(dāng)然是肯定的,對象流其實(shí)就是一種特殊的處理流水,也是在基礎(chǔ)的字節(jié)流上去作封裝。
Java采用輸入流對象和輸出流對象來支持程序?qū)?shù)據(jù)的輸入和輸出。輸入流對象提供了數(shù)據(jù)從源點(diǎn)流向程序的管道,程序可以從輸入流對象讀取數(shù)據(jù);輸出流對象提供了數(shù)據(jù)從程序流向終點(diǎn)的管道,程序通過該管道把數(shù)據(jù)寫到終點(diǎn)。所有的關(guān)于輸入/輸出的類都包含在java.io的包中。
數(shù)據(jù)流操作的是我們的基本數(shù)據(jù)類型和字符串,對象流除了基本數(shù)據(jù)類型和字符串還包括我們其它的各種對象(也包括我們自定義的對象),所以對象流是數(shù)據(jù)流的升級版。同時(shí)這個(gè)流有個(gè)特殊的叫法叫做序列化和反序列化,當(dāng)然不是所有的對象都可以序列化(必須要有通行證),必須實(shí)現(xiàn)Serializable接口。
下面程序使用一個(gè)對象流,把對象直接寫到文件中import java.io.*;
public class ObjectStreamTest {
public static void main(String[] args) throws Exception{
try {
Person P=new Person("Jeccica",26);
FileOutputStream fos=new FileOutputStream("C:\\Users\\admin\\Desktop\\Java\\temp\\22.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(P);
oos.flush();
oos.close();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
FileInputStream fis=new FileInputStream("C:\\Users\\admin\\Desktop\\Java\\temp\\22.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Person P2=(Person)ois.readObject();
System.out.println(P2.name+"的年齡為"+P2.age);
}
}
class Person implements Serializable{
String name=null;
int age=0;
Person(String _name,int _age){
name=_name;
age=_age;
}
}
對象把它輸入到字節(jié)流里面,然后存儲(chǔ)到文件,數(shù)據(jù)庫或者內(nèi)存中(內(nèi)存指的就是字節(jié)數(shù)組)。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);
}
}
//寫了一個(gè)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;
}
}
對象流其實(shí)就是能夠輸入輸出對象的流,對象流的用法也無外乎和對象相關(guān)。實(shí)際上,對象流也只不過是Java IO流中的一個(gè)很平凡的存在,在Java IO流的大家庭里還有許許多多的IO流等著我們一探究竟,快來本站觀看Java教程一起學(xué)習(xí)吧!
初級 202925
初級 203221
初級 202629
初級 203743