system.DocumentsDirectory

类型 常量
system.*
修订 版本 2024.3703
关键字 system 目录,DocumentsDirectory
请参阅 system.pathForFile()
system.ApplicationSupportDirectory
system.CachesDirectory
system.ResourceDirectory
system.TemporaryDirectory

概述

system.pathForFile() 结合使用,为需要在应用程序会话之间保持的文件的存储和检索创建路径。路径为 /Documents

此属性还可以与其他要求 baseDir 作为参数的 API 结合使用,例如 display.newImageRect()display.save() 等。

说明
  • 在 Corona Simulator 中,这将在沙盒式文件夹中具体到应用程序基础。您可以通过文件显示项目沙盒.

  • 查看其目录/文件

语法

system.DocumentsDirectory

示例

-- Get path for file "data.txt" in the documents directory
local path = system.pathForFile( "data.txt", system.DocumentsDirectory )

-- Open the file from the path
local fh, reason = io.open( path, "r" )

if fh then
    -- File exists; read its contents into a string
    local contents = fh:read( "*a" )
    print( "Contents of " .. path .. "\n" .. contents )
    
else
    -- File open failed; output the reason
    print( "File open failed: " .. reason )

    -- Create file since it doesn't exist yet
    fh = io.open( path, "w" )

    if fh then
        print( "Created file" )
    else
        print( "Create file failed!" )
    end

    local numbers = { 1,2,3,4,5,6,7,8,9 }
    fh:write( "Feed me data!\n", numbers[1], numbers[2], "\n" )

    for _,v in ipairs( numbers ) do 
        fh:write( v, " " )
    end

    fh:write( "\nNo more data\n" )
end
 
io.close( fh )