Un ObjectOutputStream écrit des types de données primitifs et des graphiques d'objets Java dans un OutputStream. Les objets peuvent être lus (reconstitués) à l'aide d'un ObjectInputStream. Le stockage persistant des objets peut être réalisé en utilisant un fichier pour le flux.
- Seuls les objets prenant en charge l'interface java.io.Serializing peuvent être écrits dans des flux. La classe de chaque objet sérialisable est codée, y compris le nom de classe et la signature de la classe, les valeurs des champs et des tableaux de l'objet et la fermeture de tout autre objet référencé à partir des objets initiaux.
- Le Java ObjectOutputStream est souvent utilisé avec un Java ObjectInputStream. ObjectOutputStream est utilisé pour écrire les objets Java et ObjectInputStream est utilisé pour relire les objets.
Constructeurs :
Méthodes :
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(); } }
Sortir :
A
La méthode correspondante dans ObjectInputStream est solveProxyClass. Pour une sous-classe donnée d'ObjectOutputStream qui remplace cette méthode, la méthode solveProxyClass dans la sous-classe correspondante d'ObjectInputStream doit lire toutes les données ou objets écrits par 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(); } }
Sortir :
tous les 3 mois
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(); } } }
Sortir :
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(); } }
Si l'activation est vraie et qu'un gestionnaire de sécurité est installé, cette méthode appelle d'abord la méthode checkPermission du gestionnaire de sécurité avec une autorisation SerialisablePermission('enableSubstitution') pour s'assurer que vous pouvez autoriser le flux à effectuer le remplacement des objets dans le flux.
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(); } }
Sortir :
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(); } }
Sortir :
A
Cette méthode n’est appelée qu’une seule fois lors de la première rencontre de chaque objet. Toutes les références ultérieures à l'objet seront redirigées vers le nouvel objet. Cette méthode doit renvoyer l'objet à remplacer ou l'objet d'origine.
Null peut être renvoyé comme objet à remplacer, mais peut provoquer NullReferenceException dans les classes contenant des références à l'objet d'origine, car elles peuvent attendre un objet au lieu de null.
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(); } }
Sortir :
GeeksforGeeks
Tous les efforts seront faits pour éviter d'introduire des incompatibilités descendantes supplémentaires ; mais parfois il n'y a pas d'autre alternative.
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(); } }
Sortir :
scanner java suivant
GeeksforGeeks
Article suivant : Classe Java.io.ObjectOutputStream en Java | Ensemble 2