Using files in J2ME

  • Thread starter joseph.paterson
  • Start date
J

joseph.paterson

Hi everyone,

I'm trying to open and read from a file using J2ME, but I seem to have
a problem.
Essentially, I do this:
try
{
input = Connector.openDataInputStream ("words");
} catch (Exception e) { this.notifyDestroyed (); }

I also tried using file://words, but I get an error. Is there a way for
figuring what directory the Class is being executed in (so I could use
file://${{PATH}words)? I looked into System.getProperty (), but found
nothing.
I also thought of putting the contents of the 'words' file directly
into my .java code, but as the file is around 600kb (it's a
dictionary...) I get a code too large error.

Also, once I do actually figure out how to open and read from this
file, should I read Byte's from the file or Char's ?

Cheers,

- Joseph Paterson
 
D

David N. Welton

Hi everyone,

I'm trying to open and read from a file using J2ME, but I seem to have
a problem.
I also tried using file://words, but I get an error. Is there a way for
figuring what directory the Class is being executed in (so I could use
file://${{PATH}words)? I looked into System.getProperty (), but found
nothing.
I also thought of putting the contents of the 'words' file directly
into my .java code, but as the file is around 600kb (it's a
dictionary...) I get a code too large error.

Also, once I do actually figure out how to open and read from this
file, should I read Byte's from the file or Char's ?

Hecl needs to do something similar, and we use a .jar "resource"

http://svn.sourceforge.net/viewcvs.cgi/hecl/trunk/hecl/j2me/Hecl.java?view=markup

Specifically, have a look at the code around

DataInputStream is = new DataInputStream(
this.getClass().getResourceAsStream("/script.hcl"));

Ciao,
--
David N. Welton
- http://www.dedasys.com/davidw/

Linux, Open Source Consulting
- http://www.dedasys.com/
 
D

Darryl L. Pierce

Hi everyone,

I'm trying to open and read from a file using J2ME, but I seem to have
a problem.
Essentially, I do this:
try
{
input = Connector.openDataInputStream ("words");
} catch (Exception e) { this.notifyDestroyed (); }

If you want to open a file *from your JAR* you need to use
MIDlet.getResourceAsStream(String) and need to prepend "/" to it.
I also tried using file://words, but I get an error.

That's because file:// is a protocol that's not supported on the handset
you're using.
Is there a way for
figuring what directory the Class is being executed in (so I could use
file://${{PATH}words)? I looked into System.getProperty (), but found
nothing.

No, you can't find that out.
Also, once I do actually figure out how to open and read from this
file, should I read Byte's from the file or Char's ?

It makes no difference, really, as far as efficiency is concerned.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top