Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User xnew's Avatar
    Join Date
    Mar 2003
    Posts
    10
    I don't know how t o read a file in j2me?
    Can anybody help me?

  2. #2
    Nokia Developer Expert jalev's Avatar
    Join Date
    Mar 2003
    Posts
    382
    Hello

    While MIDP provides methods for loading Images, it doesn't include a specific mechanism for loading files of arbitrary types. There's a simple workaround, though, that's based on three straightforward things you can do:

    You can package any type of file in the JAR file that makes up your MIDlet suite.
    You can open an InputStream to any file in your res directory.
    You can read a resource file using the getResourceAsStream() method.
    This example reads a text file into a StringBuffer, but you could easily modify it to read other types of files, including those containing binary data.

    import java.io.*;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class ReadFile extends MIDlet {
    public void startApp() {

    try {
    Class c = this.getClass();
    InputStream is = c.getResourceAsStream("/thisfile.txt");
    StringBuffer str = new StringBuffer();
    byte b[] = new byte[1];

    while ( is.read(b) != -1 ) {
    str.append(new String(b));
    }
    is.close();
    System.out.println(str);
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void destroyApp(boolean b) {}

    public void pauseApp() {}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved