Deno.writefile
writeFile
-
writeFile(path: string, data: Uint8Array, options?: WriteFileOptions): Promise<void>
-
将
data
写入给定的path
,并且根据需要创建新文件或者覆盖原文件。Write
data
to the givenpath
, by default creating a new file if needed, else overwriting.const encoder = new TextEncoder(); const data = encoder.encode("Hello world\n"); await Deno.writeFile("hello1.txt", data); // 覆盖或者创建 "hello1.txt" await Deno.writeFile("hello2.txt", data, {create: false}); // 仅当 "hello2.txt" 存在的情况下才有效 await Deno.writeFile("hello3.txt", data, {mode: 0o777}); // 设置新文件的权限 await Deno.writeFile("hello4.txt", data, {append: true}); // 在文件末尾添加数据
需要
allow-write
权限。如果options.create
为false
且需要allow-read
权限。Requires
allow-write
permission, andallow-read
ifoptions.create
isfalse
.
参数
- ##### path: string
- ##### data: Uint8Array
- ##### Optional options: [WriteFileOptions](interfaces/deno.writefileoptions.html)