đź“ť Edit page
âž• Add page
Files
See Read and write files in the manual.
Read file
Use readTextFileSync
if you don’t want to use await
.
Text
index.ts
const text = await Deno.readTextFile("test.txt"); console.log(text)
$ deno run --allow-read index.ts
Write file
Use writeTextFileSync
if you don’t want to use await
.
Text
index.ts
const output = "Hello, World!"; const path = "test.txt"; const write = await Deno.writeTextFile(path, output); console.log(`Wrote file: ${path}`);
$ deno run --allow-write index.ts
JSON
Based on example in the docs.
function writeJson(path: string, data: object): string {
try {
Deno.writeTextFileSync(path, JSON.stringify(data));
return console.log(`Wrote file: ${path}`);
} catch (e) {
return e.message;
}
}
const outData = { hello: "World" }
const outPath = "data.json"
console.log(writeJson(outPath, outData));