函数:file.readString 读文件返回字符串

函数名称:读文件返回字符串

函数功能:读文件操作,读取文件内容,返回全部内容的 string

函数方法

str = file.readString (path,encode)

参数 类型 必填 说明
path string 需要读取的文件路径,支持决定路径及相对路径,
相对路径(仅支持 v1.0.6 及其以上版本)
encode string 编码格式,不写默认为 utf-8
返回值 类型 说明
str string 文件内容

函数用例

--相对路径
--表示当前流程文件夹下的测试文件夹
path1 = "测试/"
path2 = "测试/222.lua"
--在当前流程文件夹下创建文件夹
flag1 = file.new(path1)
mSleep(5000)
--在新创建的文件夹下创建文件
flag2 = file.new(path2)
str1 = "dialog(11,5000)"
if  flag1 and flag2 then
    dialog("文件夹及文件创建成功",5000)
    --写入文件内容
    file.writeString(path2,str1)
    mSleep(2000)
  str2 = file.readString (path2,"utf-8")
    dialog("文件写入成功".."\r\n".."文件内容:"..""..str2,5000)
else
    dialog("文件(夹)创建失败或者文件(夹)已存在",5000)
end
--相对路径,仅支持 v1.0.6 及其以上版本
--相对路径为当前流程文件夹:/sdcard/LuaBox/Projects/流程名称/
path = "56.lua"
--创建文件
flag = file.new(path)
str1="dialog(1)"
mSleep(3000)
--创建文件
str1 = "dialog(11)"
if  flag then
    dialog("文件创建成功",5000)
    mSleep(3000)
    --写入文件内容
    file.writeString(path,str1)
    mSleep(2000)
    str2 = file.readString(path,"utf-8")
    dialog("文件内容:"..""..str2,5000)
else
    dialog("文件创建失败或者文件已存在",5000)
end

注意事项

  • 相对路径为当前流程文件夹:/sdcard/LuaBox/Projects/流程名称/

  • 目前积木编程函数和触动精灵函数不通用,请仔细查看本手册,此手册中函数仅支持积木编程,不支持触动精灵,同理请勿将触动精灵函数在积木编程运行。

Copyright 北京帮你玩科技有限公司 2022 all right reserved,powered by Gitbook该文章修订时间: 2023-08-09 17:19:31

results matching ""

    No results matching ""