×

Discussion Board

Results 1 to 15 of 15
  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Question How to read text file faster?

    Hello, good evening.

    I'm developing a Short Messagging Service (SMS) which correct textspeak. About the textspeak, I'm talking about the SMS language(the shortcut words). I call my program a "SMS Textspeak Corrector". I'm using text file as my database. The database contains the textspeak words along with the original words. The database comprises hundreds of textspeak and original words. My problem is in reading the text file. It is really slow and I can't find a way to make it faster. I used Nokia 6103 as my testing device.

    This is my code on how to read the text file:
    Code:
      private String file()
      {
        InputStream is = getClass().getResourceAsStream("db.txt");
        StringBuffer sb = new StringBuffer();
        try{
          int chars;
          while ((chars = is.read()) != -1)
          {
              sb.append((char) chars);
          }
          return sb.toString();
        }
        catch(Exception e)
        {
        }
        return null;
      }
    Is there any way to make it more faster?

    Hope someone can help me.

    Thanks.

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

    Re: How to read text file faster?

    Welcome to Forum Nokia!

    Have a read of this article from the wiki.

    Oh, and you might want to change "dbo.txt" to "/dbo.txt" in your call to getResourceAsStream(). You should always use an absolute path (starting with a "/"), or you can get some confusing results.

    Beware of converting bytes to chars. Remember that this really restricts the number of languages you can support.

    Graham.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Re: How to read text file faster?

    Hello grahamhughes, good morning

    Thanks for helping me grahamhughes.

    What do you mean about this line?
    you can get some confusing results.
    and this line too.
    Beware of converting bytes to chars. Remember that this really restricts the number of languages you can support.
    Can you explain this further. I'm still newbie with this. This is my first application that I made using J2ME.

    Thanks.

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

    Re: How to read text file faster?

    If your file path doesn't start with a "/", then it is relative to the path of the class on which you're calling getResourceAsStream(). Different class, or change the path to the class by running an obfuscator, and code that did work suddenly breaks.

    Bytes are 8 bits each, characters are 16. When you coerce a byte to a char, the missing 8 bits get filled in with zeros. So, you will only have the characters '\u0000' to '\u00ff'. If you want to use the full Unicode set, then you can read the entire file as a byte[], then use something like:

    Code:
    String s = new String(byteArray, "UTF-8");
    Of course, this means you needed a UTF-8 encoded file in the first place. Many text editors are able to save in this format.

    Graham.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Re: How to read text file faster?

    Hello grahamhughes, good afternoon.

    Can you give me a sample code where to put that in my code below:
    Code:
      private String file()
      {
        InputStream is = getClass().getResourceAsStream("db.txt");
        StringBuffer sb = new StringBuffer();
        try{
          int chars;
          while ((chars = is.read()) != -1)
          {
              sb.append((char) chars);
          }
          return sb.toString();
        }
        catch(Exception e)
        {
        }
        return null;
      }
    This will give me an idea on how or where to start since I'm a newbie with this.

    Thanks for helping.

  6. #6
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Re: How to read text file faster?

    Inside this line
    InputStream is = getClass().getResourceAsStream("db.txt");
    Where you had given the name of the file. Therefore the above line will be chnaged to :
    InputStream is = getClass().getResourceAsStream("/db.txt");
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  7. #7
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: How to read text file faster?

    Reading Char by Char is slow, for long files if your read it in chunks of 1024 bytes it will be faster.
    Try using public int read(byte[] b, int off, int len) throws IOException
    to bytes in bulk and append to the string buffer.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Re: How to read text file faster?

    Hello, good evening.

    Can you give me a sample code because I'm still newbie with this.

    Thanks.

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

    Re: How to read text file faster?

    Yes, there are coding example for reading from a file in the wiki article I linked you to in my original reply/.

    Graham.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Re: How to read text file faster?

    Hello, good evening.

    I'm using the this code below:
    Code:
    byte[] data = new byte[theAmountOfDataIWant];
    in.read(data);
    I believe that there's something wrong with my code when I applied because it's not working. Here's my code below:
    Code:
      private String file()
      {
          InputStream is = getClass().getResourceAsStream("/db.txt");
          byte[] sb = new byte[23];
          try
          {
              is.read(sb);
              return sb.toString();
          }
          catch(Exception e)
          {
          }
          return null;
      }
    Anybody can help me with this?

    Thanks.

  11. #11
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: How to read text file faster?

    Is db.txt in the res folder of your project and it's inside the final jar that you are deploying on the device - check filename is case sensitive too on device.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  12. #12
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: How to read text file faster?

    Hi SHENGTON,

    Take the 1KB byte array and keep reading it untill you reach the end of the bytes
    InputStream is = getClass().getResourceAsStream("/db.txt");
    byte[] sb = new byte[1024];
    StringBuffer data = new StringBuffer();
    int read = 0;
    try{
    do{
    read = in.read(sb);
    data.append(new String(sb , "utf-8"));
    }
    while(read != -1);
    }catch(Exception e){}
    return data .toString();
    You can write that some thing like this
    Last edited by bhanuchandar.k; 2011-02-10 at 18:08.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Re: How to read text file faster?

    Hello, good evening.

    @im2amit
    The "db.txt" is in the "src" folder, not in the "res" folder. Yes it is inside the final jar because that's the database and it should be included in the final jar.

    @bhanuchandar.k
    The size of the "db.txt" is 22.8KB or 23KB. Should I replace the "1024" to "23" inside the byte brackets?

  14. #14
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: How to read text file faster?

    Hi SHENGTON,

    Ya you can take how much ever you want OK . For argument shake if you take the byte array size as 23 bytes , you said your file is 23 KB the loop will run 1024 times to read the file . If you take 1024 it will run 23 times . You can take more than 1024 as well but you need to be very care full when declaring the bigger byte array . If you declare the big arrays you have more chances of getting the out of memory problems . As a stranded I have suggested that read 1KB chunks .

    Hope you got it

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Re: How to read text file faster?

    Hello bhanuchandar.k, good evening.

    Thanks for the explanation. Yeah, I got it.

Similar Threads

  1. How to read cookies into text file
    By srinivasmcs in forum Symbian
    Replies: 4
    Last Post: 2010-10-26, 10:57
  2. How to read text file from jar?
    By mohammad_kaied in forum Mobile Java General
    Replies: 3
    Last Post: 2010-10-21, 15:36
  3. how to read text file in j2me
    By sachinwarang in forum Mobile Java General
    Replies: 6
    Last Post: 2010-10-18, 19:40
  4. How to write/read a text file
    By jwon23 in forum Symbian
    Replies: 4
    Last Post: 2006-05-25, 11:13
  5. How to read normal text file??
    By namnyuck74 in forum Symbian
    Replies: 3
    Last Post: 2004-03-19, 09:03

Posting Permissions

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