Using files in J2ME

Discussion in 'Java' started by joseph.paterson, May 1, 2006.

  1. 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
     
    joseph.paterson, May 1, 2006
    #1
    1. Advertisements

  2. 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/
     
    David N. Welton, May 1, 2006
    #2
    1. Advertisements

  3. If you want to open a file *from your JAR* you need to use
    MIDlet.getResourceAsStream(String) and need to prepend "/" to it.
    That's because file:// is a protocol that's not supported on the handset
    you're using.
    No, you can't find that out.
    It makes no difference, really, as far as efficiency is concerned.
     
    Darryl L. Pierce, May 1, 2006
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.