JAVA-5 ObjectInputStream/ObjectOutputStream

其它類:

InputStream/OutputStream:https://blog.csdn.net/sinat_27180253/article/details/82185306

ByteArrayInputStream/ByteArrayOutputStream:https://blog.csdn.net/sinat_27180253/article/details/82185609

BufferedInputStream/BufferedOutputStream:https://blog.csdn.net/sinat_27180253/article/details/82185662

DataInputStream/DataOutputStream:https://blog.csdn.net/sinat_27180253/article/details/82186009

 

ObjectInputStream/ObjectOutputStream是一個裝飾流,裝飾流不能直接處理數據源,它必須和其他的裝飾流或實體流一起使用,用來實現反序列化的。

都是繼承OutputStream 和 InputStream

ObjectOutputStream 函數列表 

ObjectOutputStream(OutputStream output)

void close()

void defaultWriteObject()

void flush()

void reset()

void useProtocolVersion(int version)

void write(int value)

void write(byte[] buffer, int offset, int length)

void writeBoolean(boolean value)

void writeByte(int value)

void writeBytes(String value)

void writeChar(int value)

void writeChars(String value)

void writeDouble(double value)

void writeFields()

void writeFloat(float value)

void writeInt(int value)

void writeLong(long value)

final void writeObject(Object object)

void writeShort(int value)

void writeUTF(String value)

void writeUnshared(Object object)

 

ObjectInputStream 函數列表

ObjectInputStream(InputStream input)

int available()

void close()

void defaultReadObject()

int read(byte[] buffer, int offset, int length)

int read()

boolean readBoolean()

byte readByte()

char readChar()

double readDouble()

ObjectInputStream.GetField readFields()

float readFloat()

void readFully(byte[] dst)

void readFully(byte[] dst, int offset, int byteCount)

int readInt()

String readLine()

long readLong()

final Object readObject()

short readShort()

String readUTF()

Object readUnshared()

int readUnsignedByte()

int readUnsignedShort()

synchronized void registerValidation(ObjectInputValidation object, int priority)

int skipBytes(int length)

 

代碼寫入對象和讀取對象:

package com.stydy.stream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

public class ObjectInputStreamx {

     // 文件路徑

     public static final String filePath = "D:" + File.separator + "ZZ" + File.separator + "stream.txt";

     public static void main(String[] args) throws Exception {

          //將對象轉換爲字符流再寫入文件中

          ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath, false));

          oos.writeObject(new User("java", 16));

          oos.writeObject(new User("C++", 18));

          oos.writeObject(new User("c語言", 22));

          // 最後添加一個空對象,作爲後面讀取內容的判斷

          oos.writeObject(null);

          oos.flush();

          oos.close();

          

          // 第一次使用ObjectOutputStream往文本寫內容時,就會自動在文本內容最後打上結束標示。

          // 當再次使用ObjectOutputStream往裏面寫入內容時,內容加在上次內容的後面,當使用ObjectInputStream讀取內容時,

          // 因爲第一次寫入內容時,在後面加上了結束標示,將會讀取不到第二次寫入的內容。

          ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));

          User u;

          while ((u = (User) ois.readObject()) != null) {

              System.out.println(u);

          }

          ois.close();

     }

}

 

package com.stydy.stream;

import java.io.Serializable;

public class User implements Serializable {

     int n = 0;

     private String name;

     private int age;

     User() {

          super();

     }

     User(String name, int age) {

          super();

          n++;

          this.name = name;

          this.age = age;

     }

     

     @Override

    public String toString() {

        n--;

        return "User [name=" + name + ", age=" + age + "]";

    }

     public String getName() {

          return name;

     }

     public void setName(String name) {

          this.name = name;

     }

     public int getAge() {

          return age;

     }

     public void setAge(int age) {

          this.age = age;

     }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章