Results 1 to 10 of 10

Thread: Append to file

  1. #1
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default Append to file

    Hi all,

    Im trying to make a midlet for the S60, but im having trouble appending lines to a file.

    I've tried an OutputStream and a printStream, but all I've managed to do is overwrite existing files.

    Is there someway to append a line to a file?

    I dont want to have to read a file, copy it to a new one and then add my line. There must be an easier way, surely?

    Thanks,
    Berkeleybross

    Java Code:
    public void writeNewFile () {
            try {
                String url = System.getProperty("fileconn.dir.memorycard") + "test.txt";
                String string = "this should be on the last line";
                byte data[] = string.getBytes();
                FileConnection file = (FileConnection)Connector.open(url, Connector.READ_WRITE);
                if (!file.exists()) {
                    file.create();
                }
                OutputStream output = file.openOutputStream();
                output.write(data);
                output.close();
                file.close();
            } catch (IOException ioe) {
                System.out.println("IOException: "+ioe.getMessage());
            } catch (SecurityException se) {
                System.out.println("Security exception:" + se.getMessage());
            }
        }

  2. #2
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    Hello,

    If u want to append a file.
    then follow below steps.
    1.First u have to read that file and put all file in the StringBuffer Object
    2.Then appent the content in it
    3.then write content of that StringBuffer object in that file.
    Like this.....

    FileConnection fconn = (FileConnection)Connector.open(filepath,Connector. READ_WRITE);
    InputStream is=fconn.openDataInputStream();
    sb=new StringBuffer();
    int chars,i=0;
    while((chars=is.read())!=-1)
    {
    sb.append((char)chars);
    }
    sb.append("Hello world");
    String fileContent=new String(sb.toString());
    byte da[]=fileContent.getBytes();
    OutputStream ops = fconn.openOutputStream();
    ops.write(da);
    ops.close();
    fconn.close();

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Just use the append parameter on the FileWriter() constructor.

    -Gary-

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Hi, thanks for the replies

    @nitinverma: The code is a lot simpler than what i had thought I'd have to do, but isnt there anything like gcalvin said? A built in method to append a file?

    @gcalvin: Im presuming you mean java.io.FileWriter? I tried putting it into my code but the class was not found. I can find it in a normal project, so i presume the S60 platform does not support it. Do you know of any similar classes that can be used on the mobile?

    I've tried googling it and the best its come up with is
    Java Code:
    Connector.open("file://c:/myfile.txt;append=true", Connector.WRITE );
    However all that does is make a file called "myfile.txt;append=true", and it overwrites it when writing.

    Any more help much appreciated, failing that I guess Ill go with nitinverma's code to manually do it myself :(

    Berkeleybross

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    I've written a method based on nitinverma's code to append to a given file. I thought I'd share it with you all, incase anybody is in a similar situation.

    Java Code:
        /**
         * Used to write output to a given file. Has the option to append, which will
         * add output to the end of the file.
         * If not appending, output will be put at start of file and overwrite any bytes
         * up to the length of output.
         *
         * @param output    The string to be written to file
         * @param filePath   The full URL of the file to be written to
         * @param append    true if output should be put at end of file, false otherwise
         * @throws java.io.IOException
         */
    
        public void appendToFile (String output, String filePath, boolean append) throws java.io.IOException {
            FileConnection file = (FileConnection)Connector.open(filePath, Connector.READ_WRITE);
            byte data[];
            // Create the file if it doesnt exist
            if (!file.exists()) {
                 file.create();
                 data = output.getBytes();
            } else {
                // If append is needed, read the file and put it to the output data
                if (append) {
                    InputStream is = file.openDataInputStream();
                    StringBuffer sb = new StringBuffer();
                    int chars = 0;
                    while((chars = is.read()) != -1) {
                        sb.append((char)chars);
                    }
                    is.close();
                    // Add output to the output data
                    sb.append(output);
                    String fileContent = sb.toString();
                    data = fileContent.getBytes();
                } else {
                    // Add output to the output data
                    data = output.getBytes();
                }
            }
    
            // Write out the data to the file
            OutputStream ops = file.openOutputStream();
            ops.write(data);
            ops.close();
            file.close();
        }
    If anyone spots any mistakes with it, let me know!

    Berkeleybross

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    If your CLDC confuration has a FileOutputStream class check if it supports the 'open in append mode' constructor. If so wrap that stream in a OutputStreamWriter and you have your writer (if you need one); otherwise just use the OutputStream itself.

    kind regards,

    Jos

  7. #7
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    You can seek help from this tutorial by Java --- Files
    Hope that helps.

    Regards
    First, solve the problem. Then, write the code.
    http://www.tyroceur.co.cc ------ If my post was helpful, REP it ;)

  8. #8
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Thanks for the tips.

    @Jos unfortunately it doesnt seem to support the FileOutputStream.
    Would it be possible to add it to the jar? If so, how? (in netbeans :P)

    @tyrocer Im writing for a MIDlet not the standard java. Some of the java.io stuff is supported but the useful stuff (like append:P) doesnt seem to be.

    Thanks,
    Berkeleybross

  9. #9
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    Hey Berkeleybross,
    r u satisfy with my code or not.

  10. #10
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    @nitinverma, yes thanks. I wrote a method which is based on your code and now using it.
    It does seem a bit odd that java doesnt have a built in funtion for what is surely a common task, but oh well.
    Thanks very much for your help!

    Berkeleybross

Similar Threads

  1. How to append data to an already existing file?
    By siteregsam in forum New To Java
    Replies: 3
    Last Post: 05-03-2010, 08:06 PM
  2. How to Append in file ?
    By Hippo in forum New To Java
    Replies: 2
    Last Post: 03-19-2010, 01:50 PM
  3. Browse for and append
    By looselispssinkships in forum New To Java
    Replies: 0
    Last Post: 03-19-2010, 10:25 AM
  4. append variables to a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-02-2009, 10:17 AM
  5. JLabel append?
    By Jononomous in forum New To Java
    Replies: 0
    Last Post: 04-07-2008, 07:41 PM

Posting Permissions

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