• Post Reply Bookmark Topic Watch Topic
  • New Topic

Objectinputstreams into arrays

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

I would like to know how to output the contents of the objectinputstream into an object[] array? The input for the objectoutputstream is an object that outputs to a file called temp.txt. I would like to know how to read the stream and write to an object array as the object itself would contain either a list of strings/doubles/integers.

Any help much appreciated.

thank you.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Locate the objecst in the ObjectInputStream into a List<Object> and at the end call the toArray() method of the List.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
thanks for the reply. How do I put the contents of the objectinputstream to a list object? There isn't a readList method in the objectInputstream api.
thanks.
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using this:



but got an error:
java.lang.ClassCastException: java.lang.String
at the part.

What does this mean?

I forgot to mention that the input object is not an actual List type, it is a text file containing a list of either all doubles/strings/integers.
[ July 27, 2006: Message edited by: Angela Truce ]
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means that you serialized a String into the file, then read it out and tried to cast it to a List.
Why don't you just put the objects you want to serialize into a list when you write them out?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela, if the file you need to read is a text file containing text representation of strings and numbers, then you should not be using ObjectInputStream.

In a case like that you should be using a Reader implementation, like a BufferedReader. Read every line and perform validations to determine if it is a number or something else. And do your own conversions.



Can you, please, tell us, a bit more about the file format and how it is written?

Is it written by you own app or someone's else? are you storing serialized objects in the file, or just String representations of those objects?
[ July 27, 2006: Message edited by: Edwin Dalorzo ]
 
Angela Truce
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The object is originally from an attachmentPart. The contents of the attachment is a file which is currently a text file (have still undecided whether binary file is better). To access the contents of the attachment i use:

Then it is here that I am having trouble trying to convert the contents of this file into an array. The problem is the types in the file could be either string, double or integer so I can't just use a primitive type array and its corresponding methods.
If I ouput the contents of "input" it will look something like this:

from a text file of string:


from a text file of doubles:


from a text file of integers:


And it is the output above that I am trying to split into an array so that I can sort them later on.

Any clues on how to get them into an array in the first place?

thanks.

<added>

If I used a binary file (*.dat) generated by using my own class using the Fileoutputstream and write(byte) methods, there is a method in the ObjectInputStream called which reads the stream into an array of bytes. Do you think I could use this and then convert into an object array?
Also is it possible to sort objects anyway?

thanks again!
[ July 27, 2006: Message edited by: Angela Truce ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela, please bear on me, but I am even more confused now than before, about what your question really is.

First of all, is your question about how to read a binary file or a text file or a binary file with serialized objects?

Now I am starting to believe that your questions are how to determine the real nature of the object due to the polymorphism implicit in your method.

You have to decide on that because it is not the same thing. For instance, if store integers as Strings (i.e. "3267") in a file, they will occupy different number of bytes depending on the encoding and the number of characters. However, if you store them as binary, an integer should occupy 4 bytes, no matter what integer it is. Now if you save an java.lang.Integer object instead, you cannot even know how much space it is going to occupy.

See, it is not the same thing. And we cannot help you unless you tell us what kind of file are you dealing with.

At all events, the situation is like this:

If it is a text file, holding String representation of your characters, you will have to read every item as a String, and do some evaluation to determine if it is a integer, a double or a String. Regular expressions might help you in this case. For this case use a BufferedReader as I did before.

If it is a binary representation of data, well, simply read the file in the same order in which you wrote it. If you wrote three integers and one double and two Strings, then, when you are reading it back, do the same thing, read three integers, one double and two Strings. In this case you could use DataInputStream to simplify you life. Because it has methods for reading primitive data types from a file.

If it is a file containing serialized objects, well, it is similar. If you saved one java.lang.Double, two java.lang.Integer and one java.lang.String. Then when you are reading the file back, do the same thing. In this case you will need to use ObjectInputStream.

In every case, after converting the value read from the file into the correspongin Object you expected, simply add it to a List, and at the end get an array using the List.toArray() method.
[ July 27, 2006: Message edited by: Edwin Dalorzo ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!