2012-07-18 81 views
0

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

回答

0

你可能会想看看this blog其中介绍了如何读取和写入文件。

+0

非常感谢......我想它出来了!其实我在错误的地方寻找输出! – Vishesh 2012-07-18 14:35:42

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