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:
At first it looks promising. The
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();
}
}
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:
Thanks, this worked a treat. Exactly what I was looking for.
Glad that I could help you out.
Hi, Thank you! Really helped me! :)
Always nice to help someone out :)
Thanks for the help.
It worked for me. just a correction
instead of Long.MAX_VALUE ,it should be filecon.availableSize()
Thanks.
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.
Thanks for this awesome post. I was looking for this kind of information. Please continue writing....
Regards:-offshore software development India
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
I am not sure what your problem is. Could you please describe the problem in more details?
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
Post a Comment