Cant read a .json file from system.ResourceDirectory

Hi, I’m facing a problem with reading a file. I’ve read all documentation and forums posts I’ve found, but none of them actually helps, and I think it’s something very simple.

I’ve placed a file “en_US.json” in a Corona project folder (same as “main.lua”). Then I want to read this file in a runtime:

local fileName = “en_US.json”

mainLanguageFilePath = system.pathForFile(fileName, system.ResourceDirectory)

if(mainLanguageFilePath == nil) then

  mainLanguageFilePath = system.pathForFile(nil, system.ResourceDirectory) … “/” … fileName

end

print("Attempting to open a file: " … mainLanguageFilePath)

file = io.open(mainLanguageFilePath, “r”)

local contents = file:read("*a")

io.close(file)

stringDictionary = json.decode(contents)

It works correctly in Corona Simulator, but it is not working on Android phone. I understand that system.ResourceDirectory should be equivalent of application .zip file, and some files can be read from there only by specific APIs. But .json file should be possible to read (I’m not trying to write anything into that file). 

These lines are for Corona Simulator, without that it’s also not working on Simulator:

if(mainLanguageFilePath == nil) then

  mainLanguageFilePath = system.pathForFile(nil, system.ResourceDirectory) … “/” … fileName

end

but on Android I have an error in io.open() (passing a nil value).

Eventually I’d like to create a subdirectory for language files in system.ResourceDirectory, but I don’t know how to read those files on Android.

Have you checked if the filename is written correctly? Android is cAsE sEnSiTiVe while Windows, for isn’t.

Also, it is a good practice in general to first check if a file exists before you try to load it. Otherwise it’ll always crash, as is the case here. It’s always better to let your users know that something failed via a notification than via a crash.

Example from documentation:

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) -- Open the file handle local file, errorString = io.open( path, "r" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) else -- Read data from file local contents = file:read( "\*a" ) -- Output the file contents print( "Contents of " .. path .. "\n" .. contents ) -- Close the file handle io.close( file ) end file = nil

Thanks, XeduR @Spyric

Indeed it was a problem with the filename. I’ve resolved it a little bit different, by adding a line:

if(path ~= nil) then <<use another file>>

Have you checked if the filename is written correctly? Android is cAsE sEnSiTiVe while Windows, for isn’t.

Also, it is a good practice in general to first check if a file exists before you try to load it. Otherwise it’ll always crash, as is the case here. It’s always better to let your users know that something failed via a notification than via a crash.

Example from documentation:

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) -- Open the file handle local file, errorString = io.open( path, "r" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) else -- Read data from file local contents = file:read( "\*a" ) -- Output the file contents print( "Contents of " .. path .. "\n" .. contents ) -- Close the file handle io.close( file ) end file = nil

Thanks, XeduR @Spyric

Indeed it was a problem with the filename. I’ve resolved it a little bit different, by adding a line:

if(path ~= nil) then <<use another file>>