×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    13

    Unhappy Reading text from a text file in j2me

    Hi,

    I need to read the text from a text file in j2me.
    So now i am using below method to read the text
    private String[] file(){
    InputStream is = null;
    is = this.getClass().getResourceAsStream("help.txt");
    String s[] = null;
    String ss ="";
    try {
    int chars, i = 0;
    while ((chars = is.read()) != -1){
    ss = ss+(char)chars;
    }
    s = split(ss,"|||");
    }catch (Exception e){e.printStackTrace();}
    return(s);
    }

    In this method i have read the text file character by character. So its damn slow.
    Can any one help me out to read the file faster(line by line)?


    Advance Thanks
    Baalu

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Reading text from a text file in j2me

    The is a wiki article that might help you: How to create localized text resources.

    Cheers,
    Graham.

  3. #3
    Nokia Developer Expert
    Join Date
    May 2007
    Location
    Mexico D.F
    Posts
    351

    Re: Reading text from a text file in j2me

    Hello
    I think it's a very bad idea to append a char to a string per every single character.
    You are creating a new string object per each read... that will be slow. (remember, everytime you append two strings, another is created)

    I will recommend to use the following approach, that should be much faster

    Code:
                 InputStream is = this.getClass().getResourceAsStream("help.txt");
    
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
                    int i = 0;
                    while ((i = is.read()) != -1) {
                        baos.write(i);
                    }
    
                    response = new String(baos.toByteArray());
    Last edited by rdrincon; 2009-03-24 at 00:54.
    :Ruben

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    13

    Re: Reading text from a text file in j2me

    Hi

    Thanks for the reply,its working now

    Thanks
    Baalu

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Re: Reading text from a text file in j2me

    i want to ask how about if i want read each line without spit function with "\n" delimiter? It's possible?

Similar Threads

  1. How to append text in a text file in j2me
    By lovely_334801 in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2010-04-26, 11:10
  2. Regarding BMCONV
    By sanah007 in forum Symbian
    Replies: 2
    Last Post: 2009-05-26, 12:47
  3. [announce] PyUIQ. Python for UIQ 2.1 and 3.x
    By OscarBernabeu in forum Symbian
    Replies: 35
    Last Post: 2008-01-17, 09:12
  4. Replies: 13
    Last Post: 2007-12-07, 11:21

Posting Permissions

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