Read File from Subdirectory

I’m attempting to read a file from a directory within the resources directory of my app, and it… won’t work. I get a failed system.pathForFile() call on device. The directory is structured as follows:

[lua]
dirName
-> codefile.lua
-> config.json
[/lua]

As you can see, the directory has a Lua source file in it. Does this make Corona encrypt the contents of the directory during the build, thus making it unreadable? It seems to work when the config file is not in the directory.

Is there a way I can put config.json inside the directory but still read from it? It has to be a JSON file, so I can’t just encode the properties as Lua and require() it.

  • Caleb

Hi Caleb,

Can you post your code where you try to access this file?

Also, I don’t understand the file structure you mention. How could a Lua file have a .json file “inside” it?

dirName -\> codefile.lua-\> config.json

Thanks,

Brent

It’s just a simple file opening routine:
[lua]
local jsonFile = io.open(system.pathForFile(“dirName/config.json”), “r”)
[/lua]
It works in the simulator, but not on device. And when I inspect the contents of the built app, I can’t see the directory with the JSON file in it.

Also, sorry about the structure. That’s the forum editor doing that :(. It was originally three lines, like so:

dirName
-> codefile.lua
-> config.json

  • Caleb

Hi Caleb,

You probably shouldn’t be including a file named “config.lua” in a subdirectory. The primary “config.lua” file is used by Corona for many purposes – you can’t have multiple of these files and you should avoid using that file name elsewhere. Just name it something else like “configData.json” and that should work.

Take care,

Brent

E! Sorry again. It is JSON, not Lua. The ‘.lua’ extension sneaked in there somehow. Fixed the above post.

[EDIT:] And it also doesn’t work if I name it something different like “configData.json” instead of “config.json”. Only if it’s not inside the folder with the Lua file.

  • Caleb

Ok, it seems I’ve solved it. The directory’s name was the same as the name of the app, so when it created the executable in the app package, it couldn’t also have a directory of the same name.

Perhaps you could add an error into the build process if there’s a file or directory of the same name as the executable so this doesn’t happen in the future.

  • Caleb

Hi Caleb,

Can you post your code where you try to access this file?

Also, I don’t understand the file structure you mention. How could a Lua file have a .json file “inside” it?

dirName -\> codefile.lua-\> config.json

Thanks,

Brent

It’s just a simple file opening routine:
[lua]
local jsonFile = io.open(system.pathForFile(“dirName/config.json”), “r”)
[/lua]
It works in the simulator, but not on device. And when I inspect the contents of the built app, I can’t see the directory with the JSON file in it.

Also, sorry about the structure. That’s the forum editor doing that :(. It was originally three lines, like so:

dirName
-> codefile.lua
-> config.json

  • Caleb

Hi Caleb,

You probably shouldn’t be including a file named “config.lua” in a subdirectory. The primary “config.lua” file is used by Corona for many purposes – you can’t have multiple of these files and you should avoid using that file name elsewhere. Just name it something else like “configData.json” and that should work.

Take care,

Brent

E! Sorry again. It is JSON, not Lua. The ‘.lua’ extension sneaked in there somehow. Fixed the above post.

[EDIT:] And it also doesn’t work if I name it something different like “configData.json” instead of “config.json”. Only if it’s not inside the folder with the Lua file.

  • Caleb

Ok, it seems I’ve solved it. The directory’s name was the same as the name of the app, so when it created the executable in the app package, it couldn’t also have a directory of the same name.

Perhaps you could add an error into the build process if there’s a file or directory of the same name as the executable so this doesn’t happen in the future.

  • Caleb