I can't get file read/write working on device

I have read many topics about this problem and I am still struggling with this one. I can’t get the file reading/writing work on device. I know the sandbox is not included when building the app so I made a function where it copies whole folder from system.ResourceDirectory to system.DocumentsDirectory.

Everything works fine in simulator, but not on device. The runtime error comes when the data from the file is trying to be loaded the 1st time AFTER the my copy function (object where the data is loaded is nil when there should be data), meaning there is no files in system.DocumentsDirectory.

Weird thing is that there is no errors when I use my copyFolder function, but when I try to read from system.Directory the file.

So is the source wrong (system.ResourceDirectory) or what do I do to get file write/read working on device?

Hi @Painconfess,

Can you give us more specifics? Which device, iOS or Android? What is the runtime error specifically?

Brent

Decive is Android and the Runtime Error: (path)\main.lua:12: attempt to get length of local ‘progTable’ (a nil value).

progTable is a table what should get game progression from .json file. It is the first time program tries to read data to table, so the file does not exist and it was just supposed to be written.

Is it normal that (path) what was in error is the path where my main.lua file is in my computer?

Hi @Painconfess,

What kind of files (contents of folder) are you copying over from the ResourceDirectory to DocumentsDirectory? If it’s just some simple text files or JSON files, you should probably just create those manually when the app first loads, then update the file(s) when necessary, i.e. save game progress or whatever.

An example of creating a new file in DocumentsDirectory can be seen in this guide under “Writing Files”:

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Brent

Hi @Painconfess,

Can you give us more specifics? Which device, iOS or Android? What is the runtime error specifically?

Brent

Decive is Android and the Runtime Error: (path)\main.lua:12: attempt to get length of local ‘progTable’ (a nil value).

progTable is a table what should get game progression from .json file. It is the first time program tries to read data to table, so the file does not exist and it was just supposed to be written.

Is it normal that (path) what was in error is the path where my main.lua file is in my computer?

Hi @Painconfess,

What kind of files (contents of folder) are you copying over from the ResourceDirectory to DocumentsDirectory? If it’s just some simple text files or JSON files, you should probably just create those manually when the app first loads, then update the file(s) when necessary, i.e. save game progress or whatever.

An example of creating a new file in DocumentsDirectory can be seen in this guide under “Writing Files”:

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Brent