This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization of an Object Without Using ObjectStreams  RSS feed

 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to serialization. I have been asked to serialize an object using FileWriter. I am to create a String from the Object and write it with FileWriter. The Object is a Vector with an element that is a class with two data members. The data members are an int and a String. What is the best way to do this? I also need to reconsitute the Object. Does anyone have an example of doing this? I can do it with ObjectOutputStream and ObjectInputStream, but I don't understand what I need to do otherwise.
This is not a school assignment if anyone wonders. I am reading Roberts and Heller and Deitel and Deitel, but the practical implementation is not obvious to me.
Thanks.
[ November 07, 2002: Message edited by: Betty Schwartz ]
[ November 07, 2002: Message edited by: Betty Schwartz ]
[ November 07, 2002: Message edited by: Betty Schwartz ]
[ November 07, 2002: Message edited by: Betty Schwartz ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what is really desired here. I would say that, literally speaking, to "serialize an object using FileWriter" is impossible (or at least massively backwards). "Serialize" in Java implies a specific format defined in the Java Object Serialization Specification - which normally we don't need to think about because ObjectOutputStream and ObjectInputStream take care of it for us. If for some reason you want to replicate this format without using these classes, I suppose it's possible, but a lot of work - and you really need a byte-based OutputStream, not a Writer.
Alternately you might want to develop your own file format which can be used to save and load the state of an object - similar to what serialization does, but not literally serialization. This would take some work also, but you could customize the format to your needs. For example you might want to create a human-readable format to allow people to easily read and modify the files. XML would be a good approach here - you could use a format like this:

This represents a Vector containing two MyClass instances. You'd need to write code to read and write such files, but you can use the existing XML parsing tools to make the job easier.
Is this what you had in mind?
[ November 07, 2002: Message edited by: Jim Yingst ]
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I proposed using ObjectOutputStream and ObjectInputStream but was reminded to use the following write method provided for me. I can only assume the file will be a series of Strings that would be in human readable form. I will definitely read the Java Object Serialization Specification you provided the link to. I must also be able to retrieve the int and String members of the Vector after writing them this way.
public static synchronized void write( String filename, String contents ) throws Exception {

java.io.File outputFile = new java.io.File( filename );
FileWriter out = new FileWriter( outputFile );
out.write( contents, 0, contents.length() );
out.close();

}
 
Mary Taylor
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking to see if I can convert a file written with FileWriter to a DataInputStream to retrieve the objects that way. I'm not strong in File I/O so doing it this way is really tough.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!