Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Mar 2010

    how to append data to existing file in j2me

    Hi all,

    I am getting a problem when adding data to already existing file.It always showing me new written data only.Can anybody tell me how to append data to existing file in j2me?

    Thanks in advance

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Noida, India

    Re: how to append data to existing file in j2me

    Before writing the new data to the already existing file, place the outputstream cursor on the end of the file.

    PHP Code:

    java.io.OutputStream openOutputStream(long byteOffset)

    throws java.io.IOExceptionThis method opens an output stream and positions it at the indicated byte offset in the fileData written to the returned output stream at that position overwrites any existing data until EOF is reached, and then additional data is appendedThe connection's target must already exist and be accessible for the output stream to be created. 
    Changes made to a file through an output stream may not be immediately made to the actual file residing on the file system because platform and implementation specific use of caching and buffering of the data. Stream contents and file length extensions are not necessarily visible outside of the application immediately unless flush() is called on the stream.  The returned output stream is automatically and synchronously flushed when it is closed. 

    byteOffset - number of bytes to skip over from the beginning of the file when positioning the start of the OutputStream. If the provided offset is larger than or equal to the current file size, the OutputStream is positioned at the current end of the file for appending. 
    an open OutputStream positioned at the byte offset in the file, or the end of the file if the offset is greater than the size of the file. 
    java.io.IOException - If an I/O error occurs, if the method is invoked on a directory, the file does not yet exist, or the connection'
    s target is not accessible
    IllegalModeException - if the application does have write access to the connection's target but has opened the connection in Connector.READ mode. 
    java.lang.SecurityException - if the security if the application does not allow write access to the file. 
    java.lang.IllegalArgumentException - if byteOffset has a negative value. 

    Last edited by im2amit; 2010-04-26 at 13:17.

  3. #3
    Super Contributor
    Join Date
    Mar 2003

    Re: how to append data to existing file in j2me

    And a way to do that is via the byteOffset argument. Excerpt from the JSR-75 documentation:

    "byteOffset - number of bytes to skip over from the beginning of the file when positioning the start of the OutputStream. If the provided offset is larger than or equal to the current file size, the OutputStream is positioned at the current end of the file for appending."
    It might pay off, if you read the descriptions of the methods available for a developer using the FileConnection API. In general, many problems can be answered by reading the developer documentation, as the docs have been written for that purpose.

  4. #4
    Registered User
    Join Date
    Nov 2009

    Re: how to append data to existing file in j2me

    Refer the below code snippen to append data to existing file.

    Connection conn = Connector.open(String fileName, Connector.READ_WRITE);
    FileConnection fc = (FileConnection) conn;
    if (!fc.exists()) {
    OutputStream os = fc.openOutputStream(fc.fileSize());

    After opening the file with the above code snippet if you are going to write something to the file then it will append at the end of the file content.

    Thanks & Regards,

  5. #5
    Registered User
    Join Date
    Mar 2010

    Re: how to append data to existing file in j2me

    Thank you Mr.Ajitkumar your snippet helped me alot.


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. How to append to an existing file in J2ME ?
    By islam.abdo in forum Mobile Java General
    Replies: 2
    Last Post: 2009-06-02, 18:32
  3. Problem with GCCE build in Carbide.vs
    By raffaelbechara in forum Symbian
    Replies: 6
    Last Post: 2008-10-20, 16:06
  4. How to take file data in s String using Java and FileConnection API?
    By SanjayKhuntia in forum Mobile Java General
    Replies: 3
    Last Post: 2008-09-05, 15:22
  5. Append data to file
    By techietintu in forum Symbian
    Replies: 2
    Last Post: 2007-12-18, 05: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