0

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

3 回答 3

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()
于 2012-07-19T17:47:00.787 回答
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

于 2013-10-28T07:19:24.550 回答
0

您可能希望查看此博客,该博客描述了如何读取和写入文件。

于 2012-07-18T13:44:10.833 回答