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
David N. Welton - 01 May 2006 11:27 GMT
> 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,

Signature
David N. Welton
- http://www.dedasys.com/davidw/
Linux, Open Source Consulting
- http://www.dedasys.com/
Darryl L. Pierce - 01 May 2006 11:28 GMT
> Hi everyone,
>
[quoted text clipped - 5 lines]
> 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.

Signature
Darryl L. Pierce <mcpierce@gmail.com>
Visit my homepage: <http://mcpierce.multiply.com>
"By doubting we come to inquiry, through inquiry truth." - Peter Abelard