进阶-文件
简介
操作文件是每个语言最核心和常用的功能, 为此Z1h 语言提供了丰富的文件操作接口
用法
读文件
快速一次性读取
bytes = "文件路径".read()
写文件
快速一次性写入
"文件路径".write(bytes/string/any)
删除文件
// 注意: 如果传入的是目录路径, 将递归删除所有子文件, 需谨慎操作!
File.remove("文件路径")
遍历目录
list = File.ls("目录路径"[, "file"][, "folder"][, "info"][, "deep"][, "full"][, ".[ext]"])
文件路径相关
// 拼文件路径
File.join("目录", "子文件")
// 获取后缀
File.ext("文件路径")
// 获取绝对路径
File.abs("")
// 获取绝对路径
File.base("获取文件名")
监控文件变化
// $file.notify
// 以下的filePath可以是"xxx"也可以是["xxx", "yyy"]
$file.notify(filePath) -> watcher
$file.notify(filePath, (subFilePath, operation) => {}, "oper1", "oper2"...) -> watcher
$file.notify(filePath, {op, handler(notify/callback), ctx(context)}) -> return watcher
ctx = context()
w = $file.notify(filePath, {
handler: (op, name, ev) => {
print({ op, name }) //, str: "%+v".fmt(ev) })
},
op: ["create", "quit"],
ctx: ctx[0],
})