此运行库提供了与文件系统组件交互的通用方法。每个文件系统组件都是自己的“文件夹”,这些文件夹可以被“挂载”到全局文件目录树的某个位置,这样用户可以顺利地同时访问多个文件系统组件。请不要将此API与文件系统组件混淆,后者是此API起作用的地方。
filesystem.isAutorunEnabled(): boolean
true
,则新挂载文件系统时电脑会在其根目录中查找是否存在名为autorun[.lua]
的文件。若存在,则会执行此文件。filesystem.setAutorunEnabled(value: boolean)
filesystem.canonical(path: string): string
.
或 ..
的路径。例如, /tmp/../bin/ls.lua
和 /bin/./ls.lua
两个路径是等价的,它们的规范路径为 /bin/ls.lua
。
请注意,此函数将相对路径截断为其最顶层的“已知”目录。例如, ../bin/ls.lua
会变成 bin/ls.lua
。然而,它仍然是一个相对路径——注意开头没有斜杠。filesystem.segments(path: string): table
filesystem.segments("foo/bar")
→ {"foo","bar"}
filesystem.segments("foo/bar/../baz")
→ {"foo","baz"}
filesystem.concat(pathA: string, pathB: string[, ...]): string
fs.concat("a", "..")
的结果将是空字符串。filesystem.path(path: string): string
filesystem.name(path: string): string
filesystem.proxy(filter: string): table or nil, string
component.proxy
,区别为给定字符串也可以为文件系统组件的标签。系统将会首先检查是否存在对应标签,若不存在拥有对应标签的文件系统,系统将回退到 component.proxy
。
返回值为指定文件系统的代理对象,若未找到匹配给定过滤器的文件系统则会返回 nil
和一条报错信息。filesystem.mount(fs: table or string, path: string): boolean or nil, string
true
,若挂载失败返回 nil
和一条报错信息。filesystem.mounts(): function -> table, string
filesystem.umount(fsOrPath: table or string): boolean
filesystem.isLink(path: string): boolean[, string]
filesystem.link(target: string, linkpath: string): boolean or nil, string
filesystem.get(path: string): table, string or nil, string
nil
与一条报错信息。filesystem.exists(path: string): boolean
filesystem.size(path: string): number
filesystem.isDirectory(path: string): boolean
file.exists(path)
返回值 false ,即路径不存在。filesystem.lastModified(path: string): number
filesystem.list(path: string): function -> string or nil, string
nil
和一条报错信息。
请注意目录的路径末尾通常会加斜杠,以在不额外调用 fs.isDirectory
的情况下识别其是否为目录。filesystem.makeDirectory(path: string): boolean or nil, string
true
,失败时返回 nil
和一条报错信息。filesystem.remove(path: string): boolean or nil, string
true
,失败时返回 nil
和一条报错信息。filesystem.rename(oldPath: string, newPath: string): boolean or nil, string
true
,失败时返回 nil
和一条报错信息。filesystem.copy(fromPath: string, toPath: string): boolean or nil, string
filesystem.open(path: string[, mode: string]): table or nil, string
r
。打开模式可以为:r
、rb
、w
、wb
、a
与ab
。
成功时返回一个文件流(见下文),失败时返回 nil
和一条报错信息。
请注意你在一个文件系统中同时打开的文件数是有限的。打开的文件会在触发垃圾回收机制时自动关闭,但通常来说在文件使用完毕后调用 close
手动关闭文件流更好。io.open
而不是此函数,因为可以获得文件流的有缓冲包装版本。
使用filesystem(文件系统) API手动打开文件时会获得文件流,其为包含四个函数的表。这些函数是文件系统代理对象回调函数的轻量化包装,意味着读写操作不带缓冲,因此频繁读写少量内容时会很慢。推荐你使用 io.open
代替。
file:close()
file:read(n: number): string or nil, string
nil
。当发生错误时返回 nil
和一条报错信息。file:seek(whence: string[, offset: number]): number or nil, string
cur
(当前位置)、set
(文件流开头)或 end
(文件流结尾)。第二个参数为更改位置的偏移量。成功时返回新位置,失败时返回 nil
和一条报错信息。
第二个参数的默认值为 0,因此 f:seek("set")
会将位置重置为文件开头,f:seek("cur")
将会返回当前在文件中的位置。file:write(str: value): boolean or nil, string
true
,失败时返回 nil
和一条报错信息。