ObjectOutputStream zapiše primitivne tipe podatkov in grafe objektov Java v OutputStream. Objekte je mogoče brati (rekonstituirati) z uporabo ObjectInputStream. Trajno shranjevanje objektov je mogoče doseči z uporabo datoteke za tok.
- V tokove je mogoče zapisati samo objekte, ki podpirajo vmesnik java.io.Serializable. Razred vsakega objekta, ki ga je mogoče serializirati, je kodiran, vključno z imenom razreda in podpisom razreda, vrednostmi polj in nizov objekta ter zaključkom vseh drugih objektov, na katere se sklicujejo začetni objekti.
- Java ObjectOutputStream se pogosto uporablja skupaj z Java ObjectInputStream. ObjectOutputStream se uporablja za pisanje objektov Java, ObjectInputStream pa se uporablja za ponovno branje objektov.
Konstruktorji:
Metode:
Syntax : protected void annotateClass(Class cl) throws IOException Parameters: cl - the class to annotate custom data for Throws: IOExceptionJava
//Java program demonstrating ObjectOutputStream methods //illustrating annotateClass(Class> cl) method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) throws IOException ClassNotFoundException { FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); Character c = 'A'; //illustrating annotateClass(Class> cl) method oot.annotateClass(Character.class); //Write the specified object to the ObjectOutputStream oot.writeObject(c); //flushing the stream oot.flush(); //closing the stream oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } }
Izhod:
A
Ustrezna metoda v ObjectInputStream je resolveProxyClass. Za dani podrazred ObjectOutputStream, ki preglasi to metodo, mora metoda resolveProxyClass v ustreznem podrazredu ObjectInputStream prebrati vse podatke ali objekte, ki jih je napisal annotateProxyClass.
Syntax : protected void annotateProxyClass(Class cl) throws IOException Parameters: cl - the proxy class to annotate custom data for Throws: IOExceptionJava
//Java program demonstrating ObjectOutputStream //illustrating annotateProxyClass(Class> cl) method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) throws IOException ClassNotFoundException { FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); Character c = 'A'; //illustrating annotateProxyClass(Class> cl) method oot.annotateProxyClass(Character.class); //Write the specified object to the ObjectOutputStream oot.writeObject(c); //flushing oot.flush(); //closing the stream oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } }
Izhod:
dodajanje niza v javi
A
Syntax : public void close() throws IOException Throws: IOExceptionJava
//Java program demonstrating ObjectOutputStream //illustrating close() method import java.io.*; class ObjectOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStream oot = new ObjectOutputStream(fout); oot.write(3); //illustrating close() oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); System.out.println(oit.read()); oit.close(); } }
3
Syntax : public void defaultWriteObject() throws IOException Throws: IOExceptionJava
//Java program demonstrating ObjectOutputStream //illustrating defaultWriteObject() method import java.io.*; class ObjectOutputStreamDemo { public static void main(String[] arg) throws IOException ClassNotFoundException { Character a = 'A'; FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStream oot = new ObjectOutputStream(fout); oot.writeChar(a); oot.flush(); // close the stream oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); // reading the character System.out.println(oit.readChar()); } } class demo implements Serializable { String s = 'GeeksfoGeeks'; private void writeObject(ObjectOutputStream out) throws IOException ClassNotFoundException { //demonstrating defaultWriteObject() out.defaultWriteObject(); } } }
Izhod:
A
Syntax : protected void drain() throws IOException Throws: IOExceptionJava
//Java program demonstrating ObjectOutputStream methods //illustrating drain() method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] arg) throws IOException ClassNotFoundException { FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStream oot = new ObjectOutputStream(fout); ObjectOutputStreamDemo obj = new ObjectOutputStreamDemo(oot); //illustrating drain() obj.drain(); //closing the underlying stream oot.close(); fout.close(); } }
Če je enable nastavljen na true in je nameščen upravitelj varnosti, ta metoda najprej pokliče metodo checkPermission upravitelja varnosti z dovoljenjem SerializablePermission('enableSubstitution'), da zagotovi, da lahko tok omogoči zamenjavo predmetov v toku.
Syntax : protected boolean enableReplaceObject(boolean enable) throws SecurityException Parameters: enable - boolean parameter to enable replacement of objects Returns: the previous setting before this method was invoked Throws: SecurityExceptionJava
//Java program demonstrating ObjectOutputStream //illustrating enableReplaceObject method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) throws IOException ClassNotFoundException { FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); Character c = 'A'; //illustrating enableReplaceObject method System.out.println(oot.enableReplaceObject(true)); //Write the specified object to the ObjectOutputStream oot.writeObject(c); //flushing oot.flush(); //closing the stream oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } }
Izhod:
false A
Syntax : public ObjectOutputStream.PutField putFields() throws IOException Returns: an instance of the class Putfield that holds the serializable fields Throws: IOExceptionJava
//Java program demonstrating ObjectOutputStream //illustrating PutField method import java.io.*; class ObjectOutputStreamDemo { public static void main(String[] arg) throws IOException ClassNotFoundException { Character a ='A'; FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStream oot = new ObjectOutputStream(fout); oot.writeChar(a); oot.flush(); // close the stream oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); // reading the character System.out.println(oit.readChar()); } } class demo implements Serializable { private void writeObject(ObjectOutputStream out) throws IOException ClassNotFoundException { // Retrieve the object used to buffer // persistent fields to be written to the stream ObjectOutputStream.PutField fields = out.putFields(); } }
Izhod:
A
Ta metoda se pokliče samo enkrat, ko se vsak predmet prvič sreča. Vse nadaljnje reference na objekt bodo preusmerjene na nov objekt. Ta metoda bi morala vrniti predmet, ki ga je treba zamenjati, ali izvirni objekt.
Null se lahko vrne kot predmet, ki ga je treba zamenjati, vendar lahko povzroči izjemo NullReferenceException v razredih, ki vsebujejo sklice na izvirni objekt, saj morda pričakujejo objekt namesto ničelne vrednosti.
Syntax : protected Object replaceObject(Object obj) throws IOException Parameters: obj - the object to be replaced Returns: the alternate object that replaced the specified one Throws: IOExceptionJava
//Java program demonstrating ObjectOutputStream //illustrating replaceObject method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) throws IOException ClassNotFoundException { FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); String a = 'forGeeks'; String b = 'Geeks'; //Write the specified object to the ObjectOutputStream oot.writeObject(a); //flushing the stream oot.flush(); oot.enableReplaceObject(true); //illustrating replaceObject System.out.print(oot.replaceObject(b)); //closing the stream oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); oit.close(); } }
Izhod:
GeeksforGeeks
Potrudili se bomo, da se izognemo uvajanju dodatnih nezdružljivosti za nazaj; včasih pa ni druge možnosti.
Syntax : public void useProtocolVersion(int version) throws IOException Parameters: version - use ProtocolVersion from java.io.ObjectStreamConstants. Throws: IllegalStateException IllegalArgumentException IOExceptionJava
//Java program demonstrating ObjectOutputStream //illustrating useProtocolVersion() method import java.io.*; class ObjectOutputStreamDemo extends ObjectOutputStream { public ObjectOutputStreamDemo(OutputStream out) throws IOException { super(out); } public static void main(String[] args) throws IOException ClassNotFoundException { FileOutputStream fout = new FileOutputStream('file.txt'); ObjectOutputStreamDemo oot = new ObjectOutputStreamDemo(fout); String a = 'forGeeks'; String b = 'Geeks'; //illustrating useProtocolVersion() oot.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2); //Write the specified object to the ObjectOutputStream oot.writeObject(b); oot.writeObject(a); //flushing the stream oot.flush(); oot.close(); FileInputStream fin = new FileInputStream('file.txt'); ObjectInputStream oit = new ObjectInputStream(fin); System.out.print(oit.readObject()); System.out.print(oit.readObject()); oit.close(); } }
Izhod:
GeeksforGeeks
Naslednji članek: Razred Java.io.ObjectOutputStream v Javi | Komplet 2