Thales' cellular IoT products business is now part of Telit Cinterion, find out more.

You are here

BGS5 All data is not writen to .txt | Telit Cinterion IoT Developer Community

May 13, 2020 - 9:39am, 1383 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