Corona sdk:读一个文件

时间:2012-07-18 05:51:29

标签: lua corona

我最近开始研究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 )

3 个答案:

答案 0 :(得分:2)

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

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()

答案 1 :(得分:0)

您可能希望查看描述如何读取和写入文件的this blog

答案 2 :(得分:0)

可能这段代码会帮助你.....试试这个

 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