Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File into object stream  RSS feed

 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to output a file to an ObjectOutputStream but I don't know how to do this properly. Does the file need to be binary or can it be a text file?
What is this thing about serialisables? How do I make say a string array serialisable object?
I am very confused... !

thanks in advance.
[ August 15, 2006: Message edited by: Angela Truce ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a file by writing objects to an ObjectOutputStream that happens to be hooked up to a FileOutputStream.

You can reconstitute the objects by reading the file with an ObjectInputStream that happens to be hooked up to a FileInputStream.

The file is in a format that we are not supposed to think about. Sun warns us that a different release of the JVM may write different contents for the same object.

Serializable is an interface implemented by some classes to tell things like the ObjectOutputStream that the object is willing to be serialized. Try printing (myStringArray instanceof Serializable) to see if you can write your String array.

I did a quick Google for "java tutorial objectoutputstream" and got THIS from the Deitel father & son authors. See if it helps.

Put some code together and show us what you figure out. We'll jump in if you get stuck.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much appreciated, such lovely people on here! I will let you know how I get on.
thanks!
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was reading the tutorial and something slightly confused me. It says that "By default, array objects are serializable." So does this mean that a String [] array is automatically serializable? I tried to use a string array but it won't accept it? Do I need to parse it as an object[] array like this?

String[] strArray = new String[] {"aha","yup","i see"};
Object[] objString = (Object[]) strArray;

Or is this cheating?
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Angela Truce:
So does this mean that a String [] array is automatically serializable?


Yes. Can you show us the code and the error that you received?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does this mean that a String [] array is automatically serializable?


Frankly, I wasn't sure of this. That's why I suggested that "instanceof" exercise. Quite often the best way to answer a question is to throw a little code together and see what the compiler and the JVM do with it.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gives the following error message:


[ August 22, 2006: Message edited by: Angela Truce ]
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should try and make clear of what I am trying to do. I am running a web server that would like to receive some string input array. What I am tryin to achieve is get the fastest type that will send so thought objects would be a good idea. So I want to get an Object array sent to the server in order for it to be processed.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Angela Truce:



Ah, I see. You are trying to serialize a file. Can't be done. A file object represents an entry on the local file system. It wouldn't make sense once it popped out on the other side.
What you want to do is either serialize the data directly to the web app or use a multipart form to upload the file.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that makes more sense. How do I serialise the data directly? Do I open the text file in the serial class and output to a string array there? then convert it to an object?

Thanks.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard my previous advice. If you are using a web service, you would not be serializing the data or uploading a file. You would be using a method-call interface. You need to change your web service to accept an object (or String) array and just pass that.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
thanks. Does it matter if I pass a String or an Object? Is there a speed difference?

thanks.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not know, but I would guess that there would be no difference in passing Strings vs. Objects.
If you are really concerned about performance, I'd guess that using a multipart file upload would be faster than pretty much anything else HTTP based. Serializing any object comes with a great deal of overhead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!