类型 函数 对象 文件 库 io.* 返回值 字符串、数字或 nil
修订 发行版 2024.3703 关键字 读、文件 另请参阅 io.open() 读取和写入文件
根据指定格式读取文件,该格式指定要读取的内容。对于每种格式,该函数均返回一个具有所读字符的字符串或数字,或者若无法使用指定格式读取数据,则返回nil
。在未调用格式的情况下,它将使用默认格式读取下一整行。
File:read( [fmt1] [, fmt2] [, ...] )
-- Path for the file to read local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) -- Open the file handle local file, errorString = io.open( path, "r" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) else -- Read data from file local contents = file:read( "*a" ) -- Output the file contents print( "Contents of " .. path .. "\n" .. contents ) -- Close the file handle io.close( file ) end file = nil