08 Lua基础-读写数据文件

1.Lua在项目中的运用

  1. 作为第三方插件集成到项目
  2. 完全使用Lua进行开发,例如 quick-cocos2dx, CoronaSDK
  3. 当中一种数据的配置集(就是阵列)

2.读文件

-- 读写数据文件
local function read_files( fileName )
    -- r表示读read, w表示写write, a表示追加append 
    -- 相当于获取流的指针或句柄或对象,面向table的封装方式
    local f = assert(io.open(fileName, 'r'))

    -- *all 读取所有
    -- *line 读取一行
    -- *number 读取字符数字或者读取不超过num字符串长度
    local info = f:read("*all")
    print(info)

    -- 关闭流
    f:close();
end

read_files("love.txt")

运行结果

Dear 美玲
    我会念你冷暖,懂你悲欢。从红颜爱到白发,永远陪在你身边。
                                                        - Fadi
[Finished in 0.1s]

3.写文件

local f = assert(io.open("kiss.txt", 'w'))

f:write("I love you, 美玲")

f:close()

运行结果

写文件

猜你喜欢

转载自blog.csdn.net/su749520/article/details/80740164