×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    14

    InputStream, where is the bug?

    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
    Registered User
    Join Date
    Feb 2008
    Posts
    18

    Re: InputStream, where is the bug?

    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
    Join Date
    Mar 2003
    Location
    Barcelona, Spain
    Posts
    55

    Re: InputStream, where is the bug?

    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
  •  
×