Java: how to read and write multiple object streams to files

Source: Internet
Author: User

Idea: store serialized objects into containers (such as the serialized list <?>) And then use objectinputstream and objectoutputstream to list the instantiated objects <?> Object.

Test main program:

 
/*** @ Title: filerw. Java * @ package com. File * @ Description: exercises for creating and writing files and folders. Read/write is implemented using object streams. * @ Author jogging Android * @ date 2011-11-19 03:53:01 * @ version V1.0 */package COM. file; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. util. optional list; public class filerw {private string dirpath; private string filename; Public Static void main (string [] ARGs) {string Path = "C :\\ Xiaosheng"; string filename = "test.txt"; filerw = new filerw (path, filename ); shortlist <testmessage> msgout = new shortlist <testmessage> (); shortlist <testmessage> msgin = NULL; msgout. add (New testmessage ("Conan", "Idol"); msgout. add (New testmessage ("Kakashi", "good sample"); msgout. add (New testmessage ("android", "android"); msgout. add (New testmessage ("Haha", "test ") ); Filerw. writeobject (path, filename, msgout); msgin = filerw. readobject (path, filename); For (testmessage temp: msgin) {system. out. println (temp. getname () + temp. getdata () ;}} public filerw (string dirpath, string filename) {This. dirpath = dirpath; this. filename = filename; If (creatdir () {creatfile () ;}} private Boolean creatdir () {If (null! = Dirpath) {File Path = new file (dirpath); If (path. exists () {return true;} If (true = path. mkdirs () {return true;} return false;} private void creatfile () {If (null! = Filename) {file = new file (dirpath, filename); If (false = file. exists () {try {file. createnewfile ();} catch (ioexception e) {e. printstacktrace () ;}}}/*** @ title: writeobject * @ Description: Write a object to a file. * @ Param path the directory of the target file * @ Param filename the name of the target file * @ Param MSG the type of the object * @ return void * @ throws */private void writeobject (string path, string filename, parameter list <testmessage> MSG) {file = new file (path, filename); If (false = file. isfile () {return;} Try {// the value "false" for fileoutputstream means that overwrite this file, // if it is "true ", append the new data to this file. objectoutputstream OOS = new objectoutputstream (New fileoutputstream (file, false); OOS. writeobject (MSG); OOS. flush (); OOS. close ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}/ *** @ title: readobject * @ Description: Read a object from a file. * @ Param path the directory of the target file * @ Param filename the name of the target file * @ return parameter list <testmessage> * @ throws */@ suppresswarnings ("unchecked ") private jsonlist <testmessage> readobject (string path, string filename) {file = new file (path, filename); objectinputstream OIS = NULL; jsonlist <testmessage> msgall = NULL; try {OIS = new objectinputstream (New fileinputstream (File); try {msgall = (writable list <testmessage>) Ois. readobject ();} catch (classnotfoundexception e) {e. printstacktrace () ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {try {Ois. close ();} catch (ioexception e) {e. printstacktrace () ;}} return msgall ;}}

Definition of the message package in the test program:

/*** @ Title: testmessage. java * @ package COM. file * @ Description: the message stream of filerw * @ author jogging Android * @ date 2011-11-19 04:35:11 * @ version V1.0 */package COM. file; public class testmessage implements Java. io. serializable {private string name; private string data; Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String getdata () {return data;} public void setdata (string data) {This. data = data;} public testmessage (string name, string MSG) {This. name = Name; Data = MSG ;}}

Program running result:

References: Use of objectinputstream and objectoutputstream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.