Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User khua's Avatar
    Join Date
    Jul 2007
    Posts
    14
    Hi,
    This is the method that has problem(s), but i can't find.
    The problem :
    at first call of method it returns all chars in file, but at second and later calls it returns "".

    Code:
    public final String getchars() throws IOException {
            InputStream is=Resources.utf8file;
            StringBuffer buffer = null;                
            int ch=0; 
        try {         
          if (is == null)
            throw new Exception("File Does Not Exist");   
    
          buffer = new StringBuffer();       
         while((ch =  is.read())>-1)
             buffer.append((char) ch) ;  
           
          if (is != null)        
            is.close();
        } catch (Exception ex) {
           ex.printStackTrace();
        }
    thanks.

  2. #2
    Quote Originally Posted by khua View Post
    Hi,
    This is the method that has problem(s), but i can't find.
    The problem :
    at first call of method it returns all chars in file, but at second and later calls it returns "".

    Code:
    public final String getchars() throws IOException {
            InputStream is=Resources.utf8file;
            StringBuffer buffer = null;                
            int ch=0; 
        try {         
          if (is == null)
            throw new Exception("File Does Not Exist");   
    
          buffer = new StringBuffer();       
         while((ch =  is.read())>-1)
             buffer.append((char) ch) ;  
           
          if (is != null)        
            is.close();
        } catch (Exception ex) {
           ex.printStackTrace();
        }
    thanks.
    I think that you should open the input stream every call, currently you are getting it from Resources.utf8file;


    -------
    JavaTech
    j2me software

  3. #3
    Regular Contributor rrafols's Avatar
    Join Date
    Mar 2003
    Location
    Barcelona, Spain
    Posts
    55
    The problem could be that you're closing the stream:

    In the assignment 'InputStream is=Resources.utf8file;' you're not creating a new object you're just referencing an existing one (Resources.utf8file).

    After reading the chars in the while loop, you close the stream, so next time you try to read from it it will be closed.

    To create a new stream from a file you should use for example:

    InputStream is = getClass().getResourceAsStream(filename);

    Hope that helps.

Similar Threads

  1. N95 Bluetooth InputStream Bug?
    By mpvosseller in forum Mobile Java Networking & Messaging & Security
    Replies: 0
    Last Post: 2007-06-05, 00:48
  2. 6600 - bug in InputStream?
    By Poldipold in forum Mobile Java General
    Replies: 6
    Last Post: 2003-12-09, 03:31
  3. A bug: setColor with offscreen graphics - PLEASE, REPLY THIS TIME
    By palmcrust in forum Mobile Java General
    Replies: 11
    Last Post: 2003-12-04, 12:43
  4. Firmware bug or code bug displaying list on 3650
    By blackjack75 in forum Mobile Java General
    Replies: 2
    Last Post: 2003-09-24, 19:52
  5. InputStream zero length read bug on 7650 ?
    By robbiegates in forum Mobile Java General
    Replies: 0
    Last Post: 2002-11-08, 02:17

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