×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Question File Connection Append..

    Hi to all..

    Can somebody help me to create a file connection that appends the save text on a text file without over writing the text file when saving another text.

    I used the example in the nokia wiki with a jsr 75 api file connection..
    this is the code..
    /---------------------------------------------/
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.file.*;
    import javax.microedition.io.*;
    import java.io.*;

    public class WriteMIDlet extends MIDlet implements CommandListener {
    private TextBox textbox;
    private String photos = "fileconn.dir.photos";
    private Command saveCommand;
    private Command exitCommand;
    private String path;

    public void startApp() {
    textbox = new TextBox("WriteMIDlet", "", 1000, TextField.ANY);
    saveCommand = new Command("Save", Command.SCREEN, 1);
    exitCommand = new Command("Exit", Command.EXIT, 1);
    textbox.addCommand(saveCommand);
    textbox.addCommand(exitCommand);
    textbox.setCommandListener(this);
    Display.getDisplay(this).setCurrent(textbox);
    path = System.getProperty(photos);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    private void saveFile(String path, String name) {
    try {
    String url = path + name;
    String string = textbox.getString();
    byte data[] = string.getBytes();
    FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
    if (!fconn.exists()) {
    fconn.create();
    }
    OutputStream ops = fconn.openOutputStream();
    ops.write(data);
    ops.close();
    fconn.close();
    }
    catch (IOException ioe) {
    System.out.println("IOException: "+ioe.getMessage());
    }
    catch (SecurityException se) {
    System.out.println("Security exception:" + se.getMessage());
    }
    }

    public void commandAction(Command c, Displayable d) {
    if (c == saveCommand) saveFile(path, "readme.txt");
    if (c == exitCommand) this.notifyDestroyed();
    }
    }
    /-----------------------------------------/

    This code is working..but when you save a new text..it overwrites the text file which is under Gallery > Images in my mobile phone
    What i want to happen is that the new text will just add to the next line of the text file.

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Finland
    Posts
    9,563

    Re: File Connection Append..

    Use the file size as the offset (or the max value for the offset) to seek to the end of the file before writing more stuff to it.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Re: File Connection Append..

    can you give me code example Sir..

    tnx..

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: File Connection Append..

    Quote Originally Posted by eian@nokia View Post
    can you give me code example Sir..

    tnx..
    Use - this openOutputStream construct


    public java.io.OutputStream openOutputStream(long byteOffset) throws java.io.IOException

    This method opens an output stream and positions it at the indicated byte offset in the file. Data written to the returned output stream at that position overwrites any existing data until EOF is reached, and then additional data is appended. The 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.

    Parameters:
    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.
    Returns:
    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.
    Throws:
    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.

    Just update the following code line in your code -
    Code:
    OutputStream ops = fconn.openOutputStream(fconn.fileSize());
    Thanks,
    Ekta

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Re: File Connection Append..

    Thank you Sir..Works like a charm!

Similar Threads

  1. append to file
    By sunny_singh in forum Symbian
    Replies: 12
    Last Post: 2013-01-03, 12:58
  2. append data to file
    By golzari_ali in forum Mobile Java General
    Replies: 1
    Last Post: 2009-09-08, 21:59
  3. file connection api (append question)
    By studioj in forum Mobile Java General
    Replies: 3
    Last Post: 2009-03-31, 17:35
  4. Append data to file
    By techietintu in forum Symbian
    Replies: 2
    Last Post: 2007-12-18, 05:17
  5. append text file
    By xersmith in forum Symbian
    Replies: 1
    Last Post: 2007-02-24, 19: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
  •  
×