Terminal TC65: how to avoid data appended in file? | Telit Cinterion IoT Developer Community
June 18, 2015 - 5:19am, 2628 views
I am using addFile() method --see below-- in order to create a file in TC65 Terminal to saved a value
from servlet.I don’t have any problem to write one digit in file if the previous data saved is one digit too
but if have to write one digit and I have two digit previously saved this problem appears.
Example:
16 is the number in file saved and 3 is the new number I should get from servlet.The result I saved is 36
I presume the problem is here:
objOutputStream.write(String.valueOf(mensaje).getBytes())
write(byte[])
b(0), b(1).. This vector is the reason to have data appended. I don't know how to do a buffer refresh or something like this before saving the new data to avoid the problem I am having.Any help would be great.
Regards.
German F
public void addFile(){
FileConnection objFileConnection = null;
try {
objFileConnection = (FileConnection) Connector.open("file:///a:/dataServlet.txt");
} catch (IOException ex) {
ex.printStackTrace();
}
if(!objFileConnection.exists())
try {
objFileConnection.create();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
OutputStream objOutputStream=objFileConnection.openOutputStream();
objOutputStream.write(String.valueOf(mensaje).getBytes());
objOutputStream.flush();
objOutputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
You can add in your code where you have if(!objFileConnection.exists()), else statment inside which you delete and re create a file if it exists. So you will make sure that old data is erased and new one will be writen inside emptey file.
Regards
Jure
Hi,
Thanks a lot for your reply.I am going to check this.
Regards,
GF
Hello,
When you open the output stream for the file it is positioned at the start of the file and the existing file content is not deleted. You can also open the stream at the specified byte offset in the file with openOutputStream(long byteOffset) method.
If you want to delete the current file contents you can either delete the file or truncate it with truncate(long byteOffset) method.
Best regards,
Bartłomiej