×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    58

    Random "I/O Exception" in java me.

    Hello Everyone,

    When i open my app, i am reading 2 text files consecutively from gallery. This code works fine under normal circumstances but sometimes throws "IO Exception" or "EOF Exception". After these exception i am not getting any data from rms. Also when i reinstall the application and reading these files again, it works fine. I am not able to find out the reason why it throws these sudden exception. Please analyze my code and advice me.


    Code:
    public void simpleloginAction() {
            Connection c = null;
            OutputStream os = null;
            DataOutputStream dos = null;
            FileConnection fc = null;
            InputStream is = null;
            InputStreamReader isr = null;
            FileConnection fc1 = null;
            RecordStore rsANM = null;
            try {
                J2METable t = null;
                J2MERow row = null;            
                boolean fi = false;            
                String galleryPath = System.getProperty("fileconn.dir.photos");
                String path = galleryPath + "Test.txt";
                StringBuffer buffer = new StringBuffer();                            
                fc = (FileConnection) Connector.open(path, Connector.READ_WRITE);
                if (!fc.exists()) {
                    buffer.append("False");
                    if (fc != null) {
                        fc.close();
                    }
                } else {                                 
                    long filesize = fc.fileSize();
                    is = fc.openInputStream();
                    if (is == null) {
                        throw new Exception("File Does Not Exist");
                    }
                    isr = new InputStreamReader(is, "UTF-8"); 
                    int ch;
                    while ((ch = isr.read()) > -1) {
                        buffer.append((char) ch);
                        char bytes[] = new char[(int)filesize];                    
                        int length = isr.read(bytes, 0, (int)filesize);
                        buffer.append(new String(bytes, 0, length));                            
                    }                
                    fc.delete();
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }   
                    if (fc != null) {
                        fc.close();
                    }
                }
                
                Alert alt = new Alert("Alert");
                alt.setTimeout(5000);                
                J2MESDLIB.generateDB(false);
                path = galleryPath + "Lambit2.txt";            
                String Data = "";
                if (J2MESDLIB.existsTable("TempTable")) {
                    t = J2MESDLIB.loadTable("TempTable");
                    CountTempTable = t.rowCount();
                    if (t.rowCount() > 0) {
                        fi = t.first();
                        row = t.read();
                        Data += row.getColValue(0) + row.getColValue(1) + row.getColValue(2);
                        while ((fi = t.EOF()) == false) {  
                            fi = t.next();
                            row = t.read();
                            Data += row.getColValue(0) + row.getColValue(1) + row.getColValue(2);     
                        }
                        c = Connector.open(path, Connector.READ_WRITE); 
                        fc1 = (FileConnection) c;       
                        if (fc1.exists()) {
                            fc1.delete();
                        }
                        fc1.create();
                        os = fc1.openOutputStream(fc1.fileSize());
                        dos = new DataOutputStream(os);
                        dos.write(Data.getBytes("UTF-8"));
                        os.flush();
                        dos.flush();
                        if (os != null) {
                            os.close();
                        }
                        if (dos != null) {
                            dos.close();
                        }   
                        if (fc1 != null) {
                            fc1.close();
                        }                    
                    }
                    t.saveTable();
                    t.closeTable();
                }                                   
                J2MESDLIB.closeDB();                                            
                rsANM.closeRecordStore();    
          
                //  message sending code here. 
                //  RMS code here to display data.
    
            } catch (Exception ex) {
                Alert alt = new Alert("Alert");
                alt.setString(ex.getMessage());
                alt.setTimeout(5000);
                switchDisplayable(alt, getMainMenu());
            } finally {
                try {
                    if (c != null) {
                        c.close();
                    }
                    if (isr != null) {
                        isr.close();
                    }
                    if (is != null) {
                        is.close();
                    }  
                    if (os != null) {
                        os.close();
                    }
                    if (dos != null) {
                        dos.close();
                    } 
                    if (fc != null) {
                        fc.close();
                    }
                    if (fc1 != null) {
                        fc1.close();
                    }                
                } catch (Exception ex) {
                    Alert alt = new Alert("Alert");
                    alt.setString(ex.toString());
                    alt.setTimeout(5000);
                    switchDisplayable(alt, getMainMenu());
                }            
            }
        }

  2. #2
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: Random "I/O Exception" in java me.

    Why are you reading first byte seperatly and then doesnt use it as part of your chars reading that convert to String?
    perhaps the first byte is part of a 2byte char sequence? (for instance if its not part of the 7bit ascii characters encoding of UTF-8)
    and notice that while you treat it as a char its infact a byte.

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,699

    Re: Random "I/O Exception" in java me.

    Shai: InputStreamReader.read actually reads a character, and the loop wants to deal with the Java insanity of "read(byte/char[]) reads some bytes/characters, maybe all, maybe a few, who knows".
    On the other hand, allocating that array inside the loop is certainly sub-optimal. You may want to move it outside (and in fact it could be a smaller array). Also, creating a new String just for the append operation is something what you want to avoid, StringBuffer.append(char[],offset,length) is what you may want to use here.
    Anyway, InputStreamReader.read unlikely produce exceptions, unless there is a wrong UTF-8 code at the end of the file (e.g. a part of a multi-byte character is missing). I would also check what J2MESDLIB is doing.
    You may want to refine the Alerts, right now you can not tell which catch block is displaying the message (all of them say "Alert", even "Alert1", "Alert2" would help more).
    Last edited by wizard_hu_; 2013-09-11 at 11:00.

  4. #4
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    686

    Re: Random "I/O Exception" in java me.

    Oh i didnt notice its InputStreamReader, i dont know why but i thought it was InputStream.... sorry for the confusion.

Similar Threads

  1. Java SSL connection not working or giving "Certificate Exception:Untrusted server"
    By sdetoni in forum Mobile Java Networking & Messaging & Security
    Replies: 1
    Last Post: 2011-03-21, 12:52
  2. Replies: 4
    Last Post: 2010-08-17, 09:39
  3. Replies: 6
    Last Post: 2010-06-16, 09:21
  4. Replies: 3
    Last Post: 2010-05-24, 16:13
  5. Replies: 2
    Last Post: 1970-01-01, 02:00

Posting Permissions

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