Objectinputstream and obejctoutputstream support serialization of basic data and objects.
Objectoutputstream provides persistent storage for "Basic Data Objects". objectinputstream provides read for "Basic Data Objects.
Public class objectstreamtest {Private Static final string Path = "d :\\ baiduyun \ filetest \ rrr.txt"; public static void main (string [] ARGs) throws exception {testwrite (); testread ();} public static void testwrite () throws filenotfoundexception, ioexception {objectoutputstream out = new objectoutputstream (New fileoutputstream (new file (PATH ))); out. writeboolean (true); out. writebyte (byte) 65); out. writechar ('A'); out. writeint (0, 20131015); out. writefloat (3.14f); out. writedouble (1.414d); // write the hashmap object hashmap map = new hashmap (); map. put ("one", "Red"); map. put ("two", "green"); map. put ("three", "blue"); out. writeobject (MAP); // write the custom box object. Box implements the serializable Interface Box = new box ("desk", 80, 48); out. writeobject (box); out. close ();} public static void testread () throws filenotfoundexception, ioexception, classnotfoundexception {objectinputstream in = new objectinputstream (New fileinputstream (PATH); system. out. printf ("Boolean: % B \ n", in. readboolean (); system. out. printf ("Byte: % d \ n", (IN. readbyte () & 0xff); system. out. printf ("char: % C \ n", in. readchar (); system. out. printf ("int: % d \ n", in. readint (); system. out. printf ("float: % F \ n", in. readfloat (); system. out. printf ("Double: % F \ n", in. readdouble (); // read the hashmap object hashmap map = (hashmap) in. readobject (); iterator iter = map. entryset (). iterator (); While (ITER. hasnext () {map. entry entry = (map. entry) ITER. next (); system. out. printf ("%-6 s -- % s \ n", entry. getkey (), entry. getvalue ();} // read the box object. Box implements the serializable Interface Box = (box) in. readobject (); system. out. println ("box:" + box); In. close ();} static class box implements serializable {private int width; private int height; private string name; public box (string name, int width, int height) {This. name = Name; this. width = width; this. height = height;} @ override Public String tostring () {return "[" + name + ": (" + width + "," + height + ")] ";}}}
Objectinputstream and obejctoutputstream