MIDP -replacing the ObjectInputStream functionality

M

Maz

I've created an application that needs to read in the contents of a
text file and to insert the data into a new hashtable. (The file was
created by writing the contents of a hashtable out to a text file.)

When I was using J2SE, I had a simple method involving:

public static Hashtable readInTable(String file)
{
Hashtable htable = null;
try
{
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
htable = (Hashtable) ois.readObject();
}
catch (Exception e)
{
System.out.println(e.toString()); }
return htable;
}


What is the simplest way to do this using MIDP without the option of
using the ObjectInputStream? I've been looking at the different input
streams and the RecordStore available in MIDP but because I don't have
a lot of coding experience I'm getting totally lost!

Thanks in advance,
~M
 
D

Darryl L. Pierce,,,

Maz said:
I've created an application that needs to read in the contents of a
text file and to insert the data into a new hashtable. (The file was
created by writing the contents of a hashtable out to a text file.)

What is the simplest way to do this using MIDP without the option of
using the ObjectInputStream? I've been looking at the different input
streams and the RecordStore available in MIDP but because I don't have
a lot of coding experience I'm getting totally lost!

With the MIDP/CLDC there is no concept of object serialization. So, as
you've found out, you can't read in the text as an object. Instead, you
have to encode the data yourself when writing it to the text file and then
decode it and manually put it back into a hashtable when you need it.
 
M

Maz

you have to encode the data yourself when writing it to the text file and then
decode it and manually put it back into a hashtable when you need it.

How do I go about encoding and decoding the data to and from the text file?

Thanks in advance
~M
 
D

Darryl L. Pierce,,,

Maz said:
How do I go about encoding and decoding the data to and from the text
file?

That's completely up to you. For me, I use DataOutputStream to write the
information to a file, and then use DataInputStream to read it back in. If
you mean "how do I read a file from my MIDlet suite", you would use the
java.lang.Class.getResourceAsStream() method and attach that input stream
to your DataInputStream to read the information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top