×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    3

    Loadin a text file into a midlet

    Hi
    I have been having problems in loading a text file into a midlet
    I keep getting nullexception error and the file is in the same directory as class. I have seen res dir mention in other previous posts where exactly do I find this dir .I use Jbuilder and Nokia6310i emulator .This is the code I ve writtenpublic class ReadFile extends MIDlet {
    public void startApp() {

    try {
    Class c = this.getClass();
    InputStream is = c.getResourceAsStream("/Documents and Settings/Chibunna Mbaekube/jbproject/FileTest/classes/filetest/h.txt"
    StringBuffer str = new StringBuffer();
    byte b[] = new byte[1];
    byte input[] = new byte[34];
    int i=0;
    int offset=0;

    while ( is.read(b,offset+2,2) != -1 ) {

    input[i++] = (byte)Integer.parseInt(new String(b),16);

    }
    is.close();
    System.out.println(str);
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }

    public void destroyApp(boolean b) {}

    public void pauseApp() {}
    }

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Location
    USA, FL
    Posts
    148

    RE: Loadin a text file into a midlet

    I use the following code:

    byte [] path;
    try
    {
    int [] file_size = new int[1];
    path = LoadFile("/data/path_1.dat", file_size);
    }
    catch(Exception e)
    {
    System.out.println("ERROR: " + e.getMessage());
    return false;
    }

    public byte [] LoadFile(String resourceFileName, int [] nFileSize) throws Exception
    {
    byte [] data = null;

    try
    {
    System.out.println("Load: ["+resourceFileName+"]"

    InputStream ins = null;
    if((ins = getClass().getResourceAsStream(resourceFileName)) != null)
    {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    byte [] nextByte = new byte[1];

    while((ins.read(nextByte,0,1))!=(-1))
    {
    b.write(nextByte[0]);
    }

    if(b.size() > 0)
    {
    data = b.toByteArray();
    }

    nFileSize[0] = b.size();

    System.out.println("Loaded: " + b.size() + " byte(s)"
    }
    }
    catch(Exception e)
    {
    data = null;
    e.printStackTrace();
    }

    if(data == null)
    {
    throw new Exception("File ["+resourceFileName+"] not found!"
    }

    return data;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×