Aug 25, 2009

Appending to a File in Java ME ( J2ME )

Yesterday I was asked how to append to a file in Java ME. I have never done this before, so I could not give an answer. After some research we found a working solution. This article will go through the different ways to solve it.

I first looked into the JSR-75 documentation, but I did not find any solution there. When searching the Internet, the first solution I found was this:

public void appendToFile() {
try {
FileConnection fileConnection = (FileConnection) Connector.open(
"file:///c:/other/textfile.txt;append=true",
Connector.WRITE);
OutputStream outputStream = fileConnection.openOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.println("Some text to append");
} catch (IOException e) {
e.printStackTrace();
}
}

At first it looks promising. The Connector class specifies that you could add parameters when opening a connection. However the parameters for JSR-75 are not standardized. This solution was mentioned in the Nokia developer forums, so I guess that it works on Nokia phone. The platform that we uses did not support it. Thus this solution is not portable across different Java ME devices. I recommend that you DO NOT use a solution like this.

The next solution I found looked like this:

public void appendToFileFileSize() {

try {
FileConnection fileConnection = (FileConnection) Connector.open(
"file:///c:/other/textfile.txt", Connector.WRITE);
OutputStream outputStream = fileConnection.openOutputStream();
outputStream.flush();
long fileSize = fileConnection.fileSize();
String textToAppend = "Some text to append";
outputStream.write(textToAppend.getBytes(), (int)fileSize, textToAppend.length());
} catch (IOException e) {
e.printStackTrace();
}
}

This works but is rather clumsy and does not look clean. Note that you need to call flush() before you make a call to fileSize(), otherwise the size is not guaranteed to be correct. But wait, there is another solution.


public void appendToFileMaxLong(){
try {
FileConnection fileConnection = (FileConnection) Connector.open(
"file:///c:/other/textfile.txt", Connector.WRITE);
OutputStream outputStream = fileConnection.openOutputStream(Long.MAX_VALUE);
PrintStream printStream = new PrintStream(outputStream);
printStream.println("Some text to append");
} catch (IOException e) {
e.printStackTrace();
}
}



The method openOutputStream(long bytesOffset) open an OutputStream and moves the position the specified offset. When the offset is greater than the file size, the position is moved to the end of the file. This is an clean and portable solution.

10 comments:

DaveMc said...

Thanks, this worked a treat. Exactly what I was looking for.

My Open Source Software Development Blog said...

Glad that I could help you out.

Nerijus said...

Hi, Thank you! Really helped me! :)

My Open Source Software Development Blog said...

Always nice to help someone out :)

Raj Agrawal said...

Thanks for the help.

It worked for me. just a correction


instead of Long.MAX_VALUE ,it should be filecon.availableSize()

Thanks.

My Open Source Software Development Blog said...

It seems that many have the same problem. This is not very well described.

Please observe that the important thing here is that you set a position that is bigger than the actual file size. Thus filcon.availableSize() and Long.MAX_VALUE should both work OK.

Offshore software development India said...

Thanks for this awesome post. I was looking for this kind of information. Please continue writing....

Regards:-offshore software development India

Anonymous said...

hi i want to append the text file in j2me but i couldnot succded if you kindly help.
kindly mail me at

sanaullah.kiyani1@gmail.com

My Open Source Software Development Blog said...

I am not sure what your problem is. Could you please describe the problem in more details?

Prasmin said...

This is great.

I have some question about it and it that is it necessary to write the outstream by any other function.

Pls let me know about it