Thursday, April 14, 2011

Append String to file

 In may samples of file connection i found people facing problem when they are trying to append any string to file.

In normal case it is overwriting string to file.
use following function to solve that problem.


 public void WriteFile(String s){


        try {
                FileConnection fconn = (FileConnection)Connector.open("file:///e:/Test2.txt", Connector.READ_WRITE);

                if(!fconn.exists())
                    fconn.create();

                OutputStream os = fconn.openOutputStream(fconn.fileSize());
                DataOutputStream dos = new DataOutputStream(os);
                dos.writeUTF("Mihir");
                System.out.println("file saved");
                dos.close();
                os.close();
                fconn.close();
            }
        catch (IOException ex) {
                ex.printStackTrace();
            }
      }

No comments:

Post a Comment