【问题标题】:Corona sdk: read a fileCorona sdk:读取文件
【发布时间】:2018-07-30 03:06:06
【问题描述】:

我最近开始研究 corona sdk 并尝试读取文件并逐行打印。我到处搜索代码,但没有用……这很重要,因为我正在实习,需要尽快完成。

这是我使用的代码:

local path = system.pathForFile( "Level File Structure.txt", system. ResourceDirectory  )
local file = io.open( path, "r" )

for line in file:lines() do
  print( line )
end
io.close( file )

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    只要文件存在且路径正确,这应该可以工作;

    local path = "Level File Structure.txt", system.ResourceDirectory
    
    local function printWords()
        local file = io.open(path, "r")
        for lines in file:lines() do
            print (lines)
        end
        io.close(file)
    end
    printWords()
    

    【讨论】:

      【解决方案2】:

      您可能想查看this blog,它描述了如何读取和写入文件。

      【讨论】:

      • 非常感谢...我想通了!其实我在错误的地方寻找输出!
      【解决方案3】:

      也许这段代码会帮助你.....试试这个

       display.setStatusBar( display.HiddenStatusBar )
      -- read the file path
      local filePath = system.pathForFile( "myFile.txt", system.ResourceDirectory )
      
      local file = io.open( filePath, "r" )
      if file then
       -- read all contents of file into a string
      local contents = file:read( "*a" )
      
      print( "The file path is" .. filePath )
      print( contents )
      
      io.close( file )
      
      end
      

      更多说明请参见此处

      http://eazyprogramming.blogspot.in/2013/10/read-text-file-in-corona.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 1970-01-01
        • 2016-08-27
        • 2013-04-01
        • 2012-02-26
        • 1970-01-01
        相关资源
        最近更新 更多