codestory

Die Anleitung zu Java ObjectOutputStream

View more Tutorials:

Folge uns auf unserer fanpage, um jedes Mal benachrichtigt zu werden, wenn es neue Artikel gibt. Facebook

1- ObjectOutputStream

ObjectOutputStream ist eine Unterklasse der Klasse OutputStream. Es verwaltet ein Objekt OutputStream und stellt die Methode bereit um die primitiven Daten oder Objekt in den von ihr verwalteten OutputStream zu schreiben.

public class ObjectOutputStream
              extends OutputStream implements ObjectOutput, ObjectStreamConstants
Mit ObjectInputStream werden von ObjectOutputStream erstellte Datenquellen gelesen:
ObjectOutputStream Methods

public final void writeObject(Object obj) throws IOException
public void writeBoolean(boolean val) throws IOException
public void writeByte(int val) throws IOException
public void writeShort(int val) throws IOException
public void writeChar(int val) throws IOException
public void writeInt(int val) throws IOException
public void writeLong(long val) throws IOException
public void writeFloat(float val) throws IOException
public void writeDouble(double val) throws IOException
public void writeBytes(String str) throws IOException
public void writeChars(String str) throws IOException
public void writeUTF(String str) throws IOException

public void writeUnshared(Object obj) throws IOException
public void writeFields() throws IOException

public void defaultWriteObject() throws IOException
public void useProtocolVersion(int version) throws IOException
public ObjectOutputStream.PutField putFields() throws IOException

public void reset() throws IOException

protected void writeObjectOverride(Object obj) throws IOException  
protected void annotateClass(Class<?> cl) throws IOException  
protected void annotateProxyClass(Class<?> cl) throws IOException  
protected Object replaceObject(Object obj) throws IOException
protected boolean enableReplaceObject(boolean enable) throws SecurityException
protected void writeStreamHeader() throws IOException
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException
protected void drain() throws IOException

int getProtocolVersion()  
void writeTypeString(String str) throws IOException

// Methods Inherited from OutputStream

public void write(int val) throws IOException
public void write(byte[] buf) throws IOException
public void write(byte[] buf, int off, int len) throws IOException

public void flush() throws IOException
public void close() throws IOException
ObjectOutputStream​ Constructors

ObjectOutputStream​(OutputStream out)
Die Objekte müssen serialisiert werden, bevor sie in ObjectOutputStream geschrieben werden. Diese Objekte müssen die Interface Serializable ​​​​​​​implementieren.

2- Example 1

Die Klasse Employee implementiert die Interface Serializable, die erforderlich ist, damit sie in ObjectOutputStream geschrieben werden kann.
Employee.java

package org.o7planning.beans;

import java.io.Serializable;

public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private String fullName;
    private float salary;

    public Employee(String fullName, float salary) {
        this.fullName = fullName;
        this.salary = salary;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String firstName) {
        this.fullName = firstName;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float lastName) {
        this.salary = lastName;
    }
}
Verwenden Sie beispielweise ObjectOutputStream um die Objekte Employee in eine Datei zu schreiben.
WriteEmployeeDataEx.java

package org.o7planning.objectoutputstream.ex;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;

import org.o7planning.beans.Employee;

public class WriteEmployeeDataEx {

    // Windows: C:/Data/test/employees.data
    private static String file_path = "/Volumes/Data/test/employees.data";

    public static void main(String[] args) throws IOException {
        File outFile = new File(file_path);

        outFile.getParentFile().mkdirs();

        Employee e1 = new Employee("Tom", 1000f);
        Employee e2 = new Employee("Jerry", 2000f);
        Employee e3 = new Employee("Donald", 1200f);

        Employee[] employees = new Employee[] { e1, e2, e3 };

        OutputStream os = new FileOutputStream(outFile);
        ObjectOutputStream oos = new ObjectOutputStream(os);

        System.out.println("Writing file: " + outFile.getAbsolutePath());

        oos.writeObject(new Date());
        oos.writeUTF("Employee data"); // Some informations.

        oos.writeInt(employees.length); // Number of Employees

        for (Employee e : employees) {
            oos.writeObject(e);
        }
        oos.close();
        System.out.println("Finished!");
    }
}
Nach dem Ausführen der Klasse WriteEmployeeDataEx erhalten wir eine Datei mit verwirrendem Inhalt. Um den Inhalt zu lesen, müssen Sie die Klasse ObjectInputStream verwenden.
OK, Lesen Sie die gerade im vorherigen Schritt geschriebene Datei mit ObjectInputStream.
ReadEmployeeDataEx.java

package org.o7planning.objectoutputstream.ex;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Date;

import org.o7planning.beans.Employee;

public class ReadEmployeeDataEx {

    // Windows: C:/Data/test/employees.data
    private static String file_path = "/Volumes/Data/test/employees.data";

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        File inFile = new File(file_path);
 

        InputStream is = new FileInputStream(inFile);
        ObjectInputStream ois = new ObjectInputStream(is);

        System.out.println("Reading file: " + inFile.getAbsolutePath());
        System.out.println();

        Date date = (Date) ois.readObject();
        String info = ois.readUTF();
        
        System.out.println(date);
        System.out.println(info);
        System.out.println();
        
        int employeeCount = ois.readInt();
        
        for(int i=0; i< employeeCount; i++) {
            Employee e = (Employee) ois.readObject();
            System.out.println("Employee Name: " + e.getFullName() +" / Salary: " + e.getSalary());
        }
        ois.close();
    }
}
Output:

Reading file: /Volumes/Data/test/employees.data

Sat Mar 20 18:54:24 KGT 2021
Employee data

Employee Name: Tom / Salary: 1000.0
Employee Name: Jerry / Salary: 2000.0
Employee Name: Donald / Salary: 1200.0

3- Example 2

Die meisten Klassen in Java Collection Framework implementieren die Interface Serializable, z.B ArrayList, LinkedList, HashMap, LinkedHashMap, TreeMap,... sodass ihr Objekt in ObjectOutputStream geschrieben werden kann.
Z.B: Schreiben Sie ein Objekt ArrayList in die Datei.
HInweis: Alle Elemente von ArrayList müssen die Typ von Serializable ​​​​​​​sein.
WriteListEx1.java

package org.o7planning.objectoutputstream.ex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class WriteListEx1 {

    // Windows: C:/Data/test/flowers.data
    private static String file_path = "/Volumes/Data/test/flowers.data";

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        writeFile();

        readFile();
    }

    private static void writeFile() throws IOException {
        ArrayList<String> flowers = new ArrayList<String>();

        flowers.add("Tulip");
        flowers.add("Daffodil");
        flowers.add("Poppy");
        flowers.add("Sunflower");
        flowers.add("Bluebell");

        File file = new File(file_path);
        file.getParentFile().mkdirs();

        OutputStream os = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(os);

        // Write a String
        oos.writeUTF("A list of flowers");
        // Write an Object
        oos.writeObject(flowers);
        oos.close();
    }
    
    @SuppressWarnings("unchecked")
    private static void readFile() throws IOException, ClassNotFoundException {
        File file = new File(file_path);
        file.getParentFile().mkdirs();

        InputStream is = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(is);
        
        // Read a String
        String info = ois.readUTF();
        // Read an Object
        List<String> flowers = (List<String>) ois.readObject();

        System.out.println(info);
        System.out.println();
        for (String s : flowers) {
            System.out.println(s);
        }
        ois.close();
    }
}
Output:

A list of flowers

Tulip
Daffodil
Poppy
Sunflower
Bluebell

4- writeFields()

Angenommen, Sie haben ein Objekt GameSetting und Sie möchten dieses Objekt in ObjectOutputStream schreiben, jedoch nicht in alle Felder.
GameSetting.java

package org.o7planning.beans;

import java.io.IOException;
import java.io.ObjectOutputStream;

public class GameSetting implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private int sound;
    private int bightness;
    private String difficultyLevel;

    private String userNote;

    public GameSetting(int sound, int bightness, String difficultyLevel, String userNote) {
        this.sound = sound;
        this.bightness = bightness;
        this.difficultyLevel = difficultyLevel;
        this.userNote = userNote;
    }

    public int getSound() {
        return sound;
    }

    public int getBightness() {
        return bightness;
    }

    public String getDifficultyLevel() {
        return difficultyLevel;
    }

    public String getUserNote() {
        return userNote;
    }

    // Do not change name and parameter of this method.
    private void writeObject(ObjectOutputStream out) throws IOException {

        ObjectOutputStream.PutField fields = out.putFields();
        // Write this object with custom fields

        fields.put("sound", this.sound < 20 ? 20 : this.sound);
        fields.put("bightness", this.bightness < 30 ? 30 : this.bightness);
        fields.put("difficultyLevel", this.difficultyLevel);
        
        // Do not write "userNote".
        // fields.put("userNote", this.userNote);

        out.writeFields();
    }
}
 
ObjectOutputStream_writeFields.java

package org.o7planning.objectoutputstream.ex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;

import org.o7planning.beans.GameSetting;

public class ObjectOutputStream_writeFields {

    // Windows: C:/Data/test/game_setting.data
    private static String file_path = "/Volumes/Data/test/game_setting.data";

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        GameSetting setting = new GameSetting(10, 80, "Hard", "Try game again!");

        writeGameSetting(setting);
        readGameSetting();
    }

    private static void writeGameSetting(GameSetting setting) throws IOException {
        File file = new File(file_path);
        file.getParentFile().mkdirs();

        OutputStream os = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(os);

        // Write a String
        oos.writeUTF("Game Settings, Save at " + new Date());
        // Write Object
        oos.writeObject(setting);

        oos.close();
    }

    private static void readGameSetting() throws IOException, ClassNotFoundException {
        File file = new File(file_path);
        file.getParentFile().mkdirs();

        InputStream is = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(is);

        // Read a String
        String info = ois.readUTF();

        // Read fields
        GameSetting setting = (GameSetting) ois.readObject();

        System.out.println("sound: " + setting.getSound());
        System.out.println("bightness: " + setting.getBightness());
        System.out.println("difficultyLevel: " + setting.getDifficultyLevel());
        System.out.println("userNote: " + setting.getUserNote());  // null.

        ois.close();
    }
}
Output:

sound: 20
bightness: 80
difficultyLevel: Hard
userNote: null
Sehen Sie mehr die Methode ObjectInputStream.readFields():

5- writeUnshared​(Object)

Die Methode writeUnshared​(Object) funktioniert ähnlich wie die Methode writeObject(Object) aber sie unterscheidet sich in der folgenden Situation:
Angenommen, Sie möchten das Objekt "X" zweimal in einen ObjectOutputStream schreiben. Was passiert dann?
  1. objectOutputStream.writeObject(X);
  2. objectOutputStream.writeObject(X);
  1. Das Objekt X wird serialisiert (serialize) um das Ergebnis Y zu erhalten, und schreibt dann Y in das Ziel .
  2. Schreiben Sie eine Referenz von Y auf das Ziel.
  1. objectOutputStream.writeUnshared(X);
  2. objectOutputStream.writeUnshared(X);
  1. Das Objekt X wird serialisiert (serialize) um das Ergebnis zu erhalten, und dann schreibt Y in das Ziel.
  2. Das Objekt X wird serialisiert (serialize) um das Ergebnis zu erhalten, und dann schreibt Y in das Ziel.
Verwenden Sie beispielweise die Methode  writeObject um ein Objekt ArrayList zweimal in eine Datei zu schreiben. Verwenden Sie die Methode  writeUnshared um ein Objekt  ArrayList in der andere Datei zu schreiben. Die zweite Datei hat eine größere Umfang.
ObjectOutputStream_writeUnshared​.java

package org.o7planning.objectoutputstream.ex;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;

public class ObjectOutputStream_writeUnshared​ {

    // Windows: C:/Data/test/test1.data
    private static String file_path1 = "/Volumes/Data/test/test1.data";
    private static String file_path2 = "/Volumes/Data/test/test2.data";

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        writeObjectTest();
        
        writeUnsharedTest();
    }
    
    private static void writeObjectTest() throws IOException  {
        File file = new File(file_path1);
        file.getParentFile().mkdirs();
        
        ArrayList<String> list = new ArrayList<String>();
        list.add("One");
        list.add("Two");
        
        OutputStream os = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        
        oos.writeObject(list); // Write the first time
        oos.writeObject(list); // Write the second time
        oos.close();
    }

    private static void writeUnsharedTest() throws IOException  {
        File file = new File(file_path2);
        file.getParentFile().mkdirs();
        
        ArrayList<String> list = new ArrayList<String>();
        list.add("One");
        list.add("Two");
        
        OutputStream os = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        
        oos.writeUnshared(list); // Write the first time
        oos.writeUnshared(list); // Write the second time
        oos.close();
    }
}
Output:

View more Tutorials:

Vielleicht bist du interessiert

Das sind die Online-Courses außer der Website o7planning, die wir empfehlen. Sie können umsonst sein oder Discount haben.