Many applications need to write files to disk. Deno provides a simple interface for writing files.
const bytes = new Uint8Array([72, 101, 108, 108, 111]);
await Deno.writeFile("hello.txt", bytes, { mode: 0o644 });await Deno.writeTextFile("hello.txt", "Hello World");await Deno.writeTextFile("hello.txt", "Hello World", { encoding: "utf8" });await Deno.writeTextFile("server.log", "Request: ...", { append: true });Deno.writeFileSync("hello.txt", bytes);
Deno.writeTextFileSync("hello.txt", "Hello World");const file = await Deno.create("hello.txt");const written = await file.write(bytes);
console.log(`${written} bytes written.`);const writer = file.writable.getWriter();
await writer.write(new TextEncoder().encode("World!"));The `-W` permission is required to write files.
Run this example locally using the Deno CLI:
deno run -R -W https://docs.deno.com/examples/scripts/writing_files.ts