如何 read/get 来自 corona SDK 文件的值?

How do I read/get values from a file in corona SDK?

我目前正在使用 Corona SDK 制作应用程序。我现在的目标是创建可以存储在 .txt 文件中的内容(如字符串或布尔值)。我想做的是合而为一,例如 scores.lua 文件具有所有值,然后在需要时在 main.lua 文件中使用它们。问题是 main.lua 没有得到我保存在 scores.lua 中的文件。

我正在使用一个叫做 ego.lua

的东西
    function saveFile( fileName, fileData ) 
    local path = system.pathForFile( fileName, system.DocumentsDirectory )  
    local file = io.open( path, "w+" )


if file then
   file:write( fileData )
   io.close( file )
 end
end


function loadFile( fileName )    
local path = system.pathForFile( fileName, system.DocumentsDirectory )    
local file = io.open( path, "r" )


if file then
   local fileData = file:read( "*a" )
   io.close( file )
   return fileData  
else
   file = io.open( path, "w" )
   file:write( "empty" )
   io.close( file )
   return "empty"
  end
 end

以及我在 main.lua 文件中保存的内容:

ego = require "ego"
saveFile = ego.saveFile
loadFile = ego.loadFile

valueName = loadFile( "gucci.txt" )
local money = display.newText(tostring(valueName), 200, 100, "Helvetica", 20)

和我的 score.lua 文件:

ego = require "ego"
saveFile = ego.saveFile
loadFile = ego.loadFile

saveFile( "gucci.txt", "This works")

我推荐你 Simple-Table-Load-Save-Functions-for-Corona-SDK - 两个非常简单的加载和保存函数来存储 Lua Table 并读回。需要 Corona SDK JSON 库.