Corona sdk:读取一个文件。

我最近开始使用 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 )
点赞
用户801098
用户801098

你可能想要看看这篇博客,它描述了如何读写文件。

2012-07-18 13:44:10
用户1508495
用户1508495

这只有在文件存在并且路径正确时才有效;

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-19 17:47:00
用户2927098
用户2927098

也许这段代码可以帮到你……试试看吧。

 display.setStatusBar( display.HiddenStatusBar )
-- 读取文件路径
local filePath = system.pathForFile( "myFile.txt", system.ResourceDirectory )

local file = io.open( filePath, "r" )
if file then
 -- 将所有文件内容读取为字符串
local contents = file:read( "*a" )

print( "文件路径为:" .. filePath )
print( contents )

io.close( file )

end

想了解更多,请参考这里:

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

2013-10-28 07:19:24