Read and Write Files

Concepts

Overview

Interacting with the filesystem to read and write files is a common requirement. Deno provides a number of ways to do this via the standard library and the Deno runtime API.

As highlighted in the Fetch Data example Deno restricts access to Input / Output by default for security reasons. Therefore when interacting with the filesystem the --allow-read and --allow-write flags must be used with the deno run command.

Reading a text file

The Deno runtime API makes it possible to read text files via the readTextFile() method, it just requires a path string or URL object. The method returns a promise which provides access to the file’s text data.

Command: deno run --allow-read read.ts

  1. /**
  2. * read.ts
  3. */
  4. async function readFile(path: string): Promise<string> {
  5. return await Deno.readTextFile(new URL(path, import.meta.url));
  6. }
  7. const text = readFile("./people.json");
  8. text.then((response) => console.log(response));
  9. /**
  10. * Output:
  11. *
  12. * [
  13. * {"id": 1, "name": "John", "age": 23},
  14. * {"id": 2, "name": "Sandra", "age": 51},
  15. * {"id": 5, "name": "Devika", "age": 11}
  16. * ]
  17. */

The Deno standard library enables more advanced interaction with the filesystem and provides methods to read and parse files. The readJson() and readJsonSync() methods allow developers to read and parse files containing JSON. All these methods require is a valid file path string which can be generated using the fromFileUrl() method.

In the example below the readJsonSync() method is used. For asynchronous execution use the readJson() method.

Currently some of this functionality is marked as unstable so the --unstable flag is required along with the deno run command.

Command: deno run --unstable --allow-read read.ts

  1. /**
  2. * read.ts
  3. */
  4. import { readJsonSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
  5. import { fromFileUrl } from "https://deno.land/std@$STD_VERSION/path/mod.ts";
  6. function readJson(path: string): object {
  7. const file = fromFileUrl(new URL(path, import.meta.url));
  8. return readJsonSync(file) as object;
  9. }
  10. console.log(readJson("./people.json"));
  11. /**
  12. * Output:
  13. *
  14. * [
  15. * {"id": 1, "name": "John", "age": 23},
  16. * {"id": 2, "name": "Sandra", "age": 51},
  17. * {"id": 5, "name": "Devika", "age": 11}
  18. * ]
  19. */

Writing a text file

The Deno runtime API allows developers to write text to files via the writeTextFile() method. It just requires a file path and text string. The method returns a promise which resolves when the file was successfully written.

To run the command the --allow-write flag must be supplied to the deno run command.

Command: deno run --allow-write write.ts

  1. /**
  2. * write.ts
  3. */
  4. async function writeFile(path: string, text: string): Promise<void> {
  5. return await Deno.writeTextFile(path, text);
  6. }
  7. const write = writeFile("./hello.txt", "Hello World!");
  8. write.then(() => console.log("File written to ./hello.txt"));
  9. /**
  10. * Output: File written to ./hello.txt
  11. */

The Deno standard library makes available more advanced features to write to the filesystem. For instance it is possible to write an object literal to a JSON file.

This requires a combination of the ensureFile(), ensureFileSync(), writeJson() and writeJsonSync() methods. In the example below the ensureFileSync() and the writeJsonSync() methods are used. The former checks for the existence of a file, and if it doesn’t exist creates it. The latter method then writes the object to the file as JSON. If asynchronous execution is required use the ensureFile() and writeJson() methods.

To execute the code the deno run command needs the unstable flag and both the write and read flags.

Command: deno run --allow-write --allow-read --unstable write.ts

  1. /**
  2. * write.ts
  3. */
  4. import {
  5. ensureFileSync,
  6. writeJsonSync,
  7. } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
  8. function writeJson(path: string, data: object): string {
  9. try {
  10. ensureFileSync(path);
  11. writeJsonSync(path, data);
  12. return "Written to " + path;
  13. } catch (e) {
  14. return e.message;
  15. }
  16. }
  17. console.log(writeJson("./data.json", { hello: "World" }));
  18. /**
  19. * Output: Written to ./data.json
  20. */