xstream - objekt, der

, xstream udgør en alternativ anvendelse af java.io.objectinputstream og java.io.objectoutputstream, således at strømme af genstande, som kan være serialized eller de serialized fra xml.det er særlig nyttigt, når store grupper af genstande, som skal forarbejdes, holde et objekt i erindring på én gang. syntaks: createobjectoutputstream(), objectoutputstream objectoutputstream = xstream. createobjectoutputstream (nye fileoutputstream ("test. txt"), syntaks: createobjectinputstream(), objectinputstream objectinputstream = xstream. createobjectinputstream (nye fileinputstream ("test. txt"), lad os prøve den kode med objekt vandløb i xstream. skabe en java - klasse fil, der hedder xstreamtester i, c: \\> xstream_workspace. kom. tutorialspoint. xstream,.,,, fil: xstreamtester. java, pakke com.tutorialspoint.xstream; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import com.thoughtworks.xstream.xstream; import com.thoughtworks.xstream.annotations.xstreamalias; import com.thoughtworks.xstream.io.xml.staxdriver; offentlige klasse xstreamtester {offentlige statisk tomrum vigtigste (string - args []) (xstreamtester tester = nye xstreamtester(); xstream xstream = nye xstream (nye staxdriver()); xstream. autodetectannotations (korrekt) studerende student1 = nye studerende ("mahesh", "parashar"); studerende student2 = nye studerende ("-", "kalra"); studerende student3 = nye studerende ("...", "- -"); studerende student4 = nye studerende ("naresh", "dag"); prøve (objectoutputstream objectoutputstream = xstream.createobjectoutputstream (nye fileoutputstream ("test. txt"); objectoutputstream. writeobject (student1); objectoutputstream. writeobject (student2); objectoutputstream. writeobject (student3); objectoutputstream. writeobject (student4); objectoutputstream. writeobject ("goddag"); objectoutputstream. close(); objectinputstream objectinputstream = xstream. createobjectinputstream (nye fileinputstream ("test. txt") studerende student5 = (studerende) objectinputstream. readobject(); studerende student6 = (studerende) objectinputstream. readobject(); studerende student7 = (studerende) objectinputstream. readobject(); studerende student8 = (studerende) måltinputstream. readobject(); streng tekst = (serie) objectinputstream. readobject(); system. println (student5) system. println (student6) system. println (student7) system. println (student8) system. println (tekst);} fangst (ioexception e) (f. printstacktrace();} fangst (classnotfoundexception e) (f. printstacktrace();}}} @ xstreamalias ("studerende") klasse elev (private snor firstname; private snor lastname; offentlige studerende (string firstname, snor lastname) (this.firstname = firstname. this.lastname = lastname;} offentlige snor getfirstname() {tilbage firstname;} offentlige snor getlastname() {tilbage lastname;} offentlige snor tostring() {tilbage "elev [firstname:" + firstname + ", lastname:" + lastname + "]"}}, kontrollere resultatet, udarbejder klasser med, javac, compiler som følger: c: xstream_workspace. kom. det tutorialspoint. xstream> javac xstreamtester.java, nu kører det xstreamtester se resultatet: c:. xstream_workspace. kom. tutorialspoint. xstream> java xstreamtester, verificere output som følger:,, studerende (firstname: mahesh, lastname: parashar] - [firstname: suresh, lastname: kalra] - [firstname: ramesh, lastname: kumar] studerende [firstname: naresh, lastname: sharma. hej, se på indholdet af de test.txt til stede, c:.> xstream_workspace. kom. tutorialspoint. xstream, mappe.,, <?xml - version = "0"?> <object-stream> <student> <firstName>Mahesh</firstName> <lastName>Parashar</lastName> </student> <student> <firstName>Suresh</firstName> <lastName>Kalra</lastName> </student> <student> <firstName>Ramesh</firstName> <lastName>Kumar</lastName> </student> <student> <firstName>Naresh</firstName> <lastName>Sharma</lastName> </student> <string>Hello World</string> </object-stream> ,



Previous:
Next Page: