cabinet 是一个文件管理的包,我们可以在 Deno 代码中使用 cabinet 更优雅的管理文件。
使用方式
import {
Cabinet,
CabinetFile,
CabinetError,
} from "https://deno.land/x/cabinet/mod.ts";
// 创建 Cabinet 类
const file = new Cabinet("./testing.txt");
// 向文件写入内容
file.write("The date is " + new Date());
file.write("The date is " + new Date(), () => console.log("finished!"));
file.writer.sync("This was written synchronously");
file.writer
.promise("This was written with a promise")
.then(() => console.log("finished!"));
file.writer.callback("This was written with a callback", () =>
console.log("finished!")
);
file.read();
file.read(() => console.log("finished!"));
file.reader.sync();
file.reader.promise().then((cFile: CabinetFile) => {
console.log("read " + cFile.size.mb + "mb in promise");
});
file.reader.callback((err?: CabinetError, cFile?: CabinetFile) => {
console.log("read " + cFile?.size.mb + "mb in callback");
});