如何找到或放置文件以便能够在Corona中读取和写入?

时间:2012-06-25 10:00:40

标签: file lua corona

这是Peach Ellen的ego.lua文件,它读取和写入文件。但是,我不知道在哪里放置我的txt文件以便在其上进行读写。我将文件放入项目文件中,并将其拖动到项目所在的日冕。但是,它不会对变化做出反应。

   module (..., package.seeall)

  --Save function
  function saveFile( fileName, fileData )
         --Path for file
         local path = system.pathForFile( fileName, system.DocumentsDirectory )
         --Open the file
         local file = io.open( path, "w+" )

    --Save specified value to the file
        if file then
          file:write( fileData )
          io.close( file )
       end
   end

  --Load function
  function loadFile( fileName )
  --Path for file
  local path = system.pathForFile( fileName, system.DocumentsDirectory )
  --Open the file
  local file = io.open( path, "r" )

    --If the file exists return the data
    if file then
       local fileData = file:read( "*a" )
       io.close( file )
       return fileData
    --If the file doesn't exist create it and write it with "empty"
    else
       file = io.open( path, "w" )
       file:write( "empty" )
       io.close( file )
       return "empty"
    end
 end

这是使用ego.lua并尝试记录最高分的main.lua文件:

 --Hide the status bar
 display.setStatusBar(display.HiddenStatusBar)

 --Saving/Loading Stuff
 local ego = require "ego"
 local saveFile = ego.saveFile
 local loadFile = ego.loadFile
  --------------------------------------------------------------------
  --------------------------------------------------------------------

 --Create the background and make it blue
 local bg = display.newRect( 0, 0, 320, 480 )
 bg:setFillColor( 150, 180, 200 )

 --Start the score at 0
 local score = 0

 --Create score text and make it dark gray
 local scoreText = display.newText(score, 200, 20, native.systemFont, 24)
 scoreText:setTextColor( 80, 80, 80 )

 --Function to add to score and update scoreText
  local function addToScore()
  score = score + 1
  scoreText.text = score
end
 bg:addEventListener("tap", addToScore)

  --------------------------------------------------------------------
  --------------------------------------------------------------------

  --Load highscore value from file. (It will initally be a string.)
   highscore = loadFile ("gameScores.txt")

  --If the file is empty (this means it is the first time you've run the app) save it as 0
   local function checkForFile ()
   if highscore == "empty" then
   highscore = 0
   saveFile("gameScores.txt", highscore)
     end
  end
  checkForFile()

  --Print the current highscore
  print ("Highscore is", highscore)

   --------------------------------------------------------------------
   --------------------------------------------------------------------

  --When the app is quit (or simulator refreshed) save the new highscore
  --(If score > highscore the data will not be changed)

  local function onSystemEvent ()
  if score > tonumber(highscore) then --We use tonumber as highscore is a string when loaded
  saveFile("gameScores.txt", score) 
   end
 end

 Runtime:addEventListener( "system", onSystemEvent )

1 个答案:

答案 0 :(得分:2)

您可以看到Ego类将其所有文件存储在system.DocumentsDirectory目录中。

此目录可通过文件访问 - >显示项目沙箱 - >文件

如果需要,您可以将目录更改为其他目录。 可用的目录是

system.ResourceDirectory

system.DocumentsDirectory

system.TemporaryDirectory

system.CachesDirectory