Saturday, April 30, 2011

Setting JAVA_HOME in Ubuntu

I have read lots of user posting at various pages and none of them would work. Finally, I found a way to do this correctly and hope this will help to some of you.

Follow the simple steps:

1. To set the environment variables :
    echo ‘export JAVA_HOME=/opt/jdk1.5.0_12′ > /etc/profile.d/jdk.sh
    echo ‘export PATH=$JAVA_HOME/bin:$PATH’ >> /etc/profile.d/jdk.sh

2. You have to source the file you just created by typing:
    source /etc/profile.d/jdk.sh

3. Test if Java environment is successfully installed by typing in this in the shell:
    java -version

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();
            }
      }