BGS5 All data is not writen to .txt | Telit Cinterion IoT Developer Community
May 13, 2020 - 9:39am, 1384 views
Hello,
I get some .json over HTTP POST that I want to write to gemalto storage. But the problem is that all the content is not wirten to the file.
I output content and it's length before I write it and in the last case it's length was: 17039 (length of string)
And I checked the file latter and it had only 7741 characters...
Here is the code that I use, I think that it is based on the tutorial that I found on this forum:
static public void writeToFile(String content, String file) throws IOException //pisanja v datoteko
{
try
{
String filename_new = "file:///a:/" + file; // mesto datoteke
FileConnection fc;
System.out.println("opening FileConnection... " + file);
fc = (FileConnection) Connector.open(filename_new, Connector.READ_WRITE);
if (fc.exists())
{
fc.delete();
fc.close();
}
fc = (FileConnection) Connector.open(filename_new, Connector.READ_WRITE);
fc.create();
OutputStream os = fc.openOutputStream(fc.fileSize());
System.out.println("WRITING TO FILE, CONTENT LENGTH: " + content.length() + "\nCONTENT: " + content);
for (int i = 0; i < content.length(); i++)
{
os.write(content.charAt(i));
}
System.out.println("closing fc... " + file);
os.close();
fc.close();
}
catch(Exception ex)
{
System.out.println("Exception on writeToFile: " + ex.toString());
}
}
Regards,
Jure
Hello,
The old file is deleted in your code each time. Are you sure that the file you verify was written in the same method call where 17039 characters should have been written? Please also debug your code for each write operation to display the written character. BTW the code may not be efficient for long strings as it writes character by character. You may consider using com.sun.midp.io.BufferedOutputStream and also using a buffer for writing instead of a single charater.
Regards,
Bartłomiej
Yes file was writen in same method call. Thank you for note about efficency, I am using BufferedOutputStream now and problem is gone.
Here is the example if it will help anyone:
Regards,
Jure