2020-08-26 12:45:03 -04:00
|
|
|
# Read and Write Files
|
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
## Concepts
|
|
|
|
|
|
|
|
- Deno's runtime API provides the
|
|
|
|
[Deno.readTextFile](https://doc.deno.land/builtin/stable#Deno.readTextFile)
|
|
|
|
and
|
|
|
|
[Deno.writeTextFile](https://doc.deno.land/builtin/stable#Deno.writeTextFile)
|
|
|
|
asynchronous functions for reading and writing entire text files
|
|
|
|
- Like many of Deno's APIs, synchronous alternatives are also available. See
|
|
|
|
[Deno.readTextFileSync](https://doc.deno.land/builtin/stable#Deno.readTextFileSync)
|
|
|
|
and
|
|
|
|
[Deno.writeTextFileSync](https://doc.deno.land/builtin/stable#Deno.writeTextFileSync)
|
|
|
|
- Use `--allow-read` and `--allow-write` permissions to gain access to the file
|
|
|
|
system
|
|
|
|
|
|
|
|
## 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
|
2020-08-26 12:45:03 -04:00
|
|
|
[standard library](https://deno.land/std) and the
|
|
|
|
[Deno runtime API](https://doc.deno.land/builtin/stable).
|
|
|
|
|
|
|
|
As highlighted in the [Fetch Data example](./fetch_data) Deno restricts access
|
2020-09-12 08:03:18 -04:00
|
|
|
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.
|
2020-08-26 12:45:03 -04:00
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
## Reading a text file
|
2020-08-26 12:45:03 -04:00
|
|
|
|
|
|
|
The Deno runtime API makes it possible to read text files via the
|
2020-09-17 11:26:46 -04:00
|
|
|
`Deno.readTextFile()` method, it just requires a path string or URL object. The
|
2020-08-26 12:45:03 -04:00
|
|
|
method returns a promise which provides access to the file's text data.
|
|
|
|
|
|
|
|
**Command:** `deno run --allow-read read.ts`
|
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
```typescript
|
|
|
|
/**
|
|
|
|
* read.ts
|
|
|
|
*/
|
2020-09-17 11:26:46 -04:00
|
|
|
const text = Deno.readTextFile("./people.json");
|
2020-08-26 12:45:03 -04:00
|
|
|
|
|
|
|
text.then((response) => console.log(response));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output:
|
|
|
|
*
|
|
|
|
* [
|
|
|
|
* {"id": 1, "name": "John", "age": 23},
|
|
|
|
* {"id": 2, "name": "Sandra", "age": 51},
|
|
|
|
* {"id": 5, "name": "Devika", "age": 11}
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
```
|
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
## Writing a text file
|
2020-08-26 12:45:03 -04:00
|
|
|
|
|
|
|
The Deno runtime API allows developers to write text to files via the
|
2020-09-17 11:26:46 -04:00
|
|
|
`Deno.writeTextFile()` method. It just requires a file path and text string. The
|
2020-08-26 12:45:03 -04:00
|
|
|
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`
|
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
```typescript
|
|
|
|
/**
|
|
|
|
* write.ts
|
|
|
|
*/
|
2020-09-17 11:26:46 -04:00
|
|
|
const write = await Deno.writeTextFile("./hello.txt", "Hello World!");
|
2020-08-26 12:45:03 -04:00
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
write.then(() => console.log("File written to ./hello.txt"));
|
2020-08-26 12:45:03 -04:00
|
|
|
|
|
|
|
/**
|
2020-09-12 08:03:18 -04:00
|
|
|
* Output: File written to ./hello.txt
|
2020-08-26 12:45:03 -04:00
|
|
|
*/
|
|
|
|
```
|
|
|
|
|
2020-09-17 11:26:46 -04:00
|
|
|
By combining `Deno.writeTextFile` and `JSON.stringify` you can easially write
|
|
|
|
serialized JSON objects to a file. This example uses synchronous
|
|
|
|
`Deno.writeTextFileSync`, but this can also be done asynchronously using
|
|
|
|
`await Deno.writeTextFile`.
|
2020-08-26 12:45:03 -04:00
|
|
|
|
2020-09-17 11:26:46 -04:00
|
|
|
To execute the code the `deno run` command needs the write flag.
|
2020-08-26 12:45:03 -04:00
|
|
|
|
2020-09-17 11:26:46 -04:00
|
|
|
**Command:** `deno run --allow-write write.ts`
|
2020-08-26 12:45:03 -04:00
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
```typescript
|
|
|
|
/**
|
|
|
|
* write.ts
|
|
|
|
*/
|
2020-08-26 12:45:03 -04:00
|
|
|
function writeJson(path: string, data: object): string {
|
|
|
|
try {
|
2020-09-17 11:26:46 -04:00
|
|
|
Deno.writeTextFileSync(path, JSON.stringify(data));
|
2020-08-26 12:45:03 -04:00
|
|
|
|
|
|
|
return "Written to " + path;
|
|
|
|
} catch (e) {
|
|
|
|
return e.message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(writeJson("./data.json", { hello: "World" }));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output: Written to ./data.json
|
|
|
|
*/
|
|
|
|
```
|