Deno Tutorial
Deno File Handling
File Handling is an easy task with Deno.
With Deno, you can read, write, append, remove any file like other server-side runtime languages.
The Deno
namespace provides quite some numbers of Filesystem APIs to get this done.
Deno Reading a File
To open and read a file with Deno, we can use open()
and readAll()
method of the Deno
namespace.
Deno Read File With open()
Deno Example Read File With open()
const file = await Deno.open('intro.txt');
const decoder = new TextDecoder('utf-8');
const text = decoder.decode(await Deno.readAll(file));
console.log(text);
Example Explained
open()
returns apromise
that can be resolved toFile
object.- a decoder is created with
TextDecoder('utf8')
that decodesUint8Array
to human-readable texts. readAll()
returns the text (String
) from aUint8Array
object.
Deno Read File With readFile()
Another way to read files in Deno is by using the readFile()
method of the Deno
namespace.
Deno Example Read File With readFile()
// Create a UTF8 decoder
const decoder = new TextDecoder('utf-8');
// Read from file and decode
const text = decoder.decode(await Deno.readFile('intro.txt'));
// Display texts (String)
console.log(text)
Example Explained
- We first create a decoder with
TextDecoder('utf8')
and store it indecoder
constant, this decodesUint8Array
to human-readable texts. - With the read from the file and then decode it in one-line with
Deno.readFile()
anddecoder.decode()
respectively. console.log(text)
displays the decoded strings.
Deno Blocking Read File With readFileSync()
The Deno
namespace also provides the readFileSync()
method that reads a file in a blocking way. This means that the file handling operation will have to wait for the process the next lines of code can be executed. Typically the opposite of what was explained in the Note above.
Deno Example Blocking Read File With readFileSync()
// Create a UTF8 decoder
const decoder = new TextDecoder('utf-8');
// Read from file and decode
const text = decoder.decode(await Deno.readFileSync('intro.txt'));
// Display texts (String)
console.log(text)
Deno Writing a File
To open and write a file with Deno is easy.
We can use the writeTextFile()
, writeFile()
and writeFileSync()
from the built-in Deno
namespace, or import writeFileStr()
, writeFileStrSync()
from the std/fs
module.
There are two major ways to writing files in Deno.
- Writing a string or text - uses
writeTextFile()
,writeFile()
,writeFileSync()
orwriteFileStrSync()
from thestd/fs
module. - Writing a binary file - uses
writeFile()
orwriteFileSync()
Deno Write Text to File with writeTextFile()
writeTextFile(path, data)
writeTextFile()
asynchronously (non-blocking) write string data to the given path. If the file already exists, it overwrites it, else it creates a new one.
Deno Example Write File With writeTextFile()
const file_path = 'intro-write.txt'
// Write to file async non-blocking
await Deno.writeTextFile(file_path, 'Writing text is simple');
// Read the file to see if it truly wrties.
console.log(await Deno.readTextFile(file_path));
Example Explained
- The
file_path
constant stores the string which is the path to the file. - The
writeTextFile()
asynchronously writes the text string into the file path.
Deno Blocking Write Text to File with writeTextFileSync()
writeTextFileSync(path, data)
writeTextFileSync()
synchronously (blocking) write string data to the given path. If the file already exists, it overwrites it, else it creates a new one.
Deno Example Write File With writeTextFileSync()
const file_path = 'intro-write.txt'
// Write to file blocking, Deno will wait for this process before executing next lines of code
Deno.writeTextFileSync(file_path, 'Writing text is simple.');
// Read the file to see if it truly wrties.
console.log(Deno.readTextFileSync(file_path));
Example Explained
- The
file_path
constant stores the string which is the path to the file. - The
writeTextFileSync()
synchronously writes the text string into the file path.
Deno Write to a File Using writeFileStr() or writeFileStrAsync()
Because the writeFileStr()
and writeFileStrAsync()
are form the std/fs
, we are going to be importing them to use them.
Deno Example Write File with writeFileStr() and writeFileStrSync()
// import the writeFileStr and writeFileStrSync methods from the std/fs module
import { writeFileStr, writeFileStrSync } from 'https://deno.land/std/fs/write_file_str.ts'
// Write file asynchronously
await writeFileStr('intro-write.txt', 'Another simple method to write file with Deno');
// Use blocking method
writeFileStrSync('intro-write.txt', 'Another simple method to write file with Deno (blocking).');
// Read the file to see if it truly wrties.
console.log(await Deno.readTextFile('intro-write.txt'));
Deno Append to a File
To append a file in Deno, we use the same approach as above. We just need to specify that in the WriteFileOptions
object optional parameter. We can append to a file by setting the append
property of the WriteFileOptions
parameter to true
.
writeTextFile(path, data, {"append": true})
Let's append to the intro-write.txt file.
Deno Removing File
As Deno covers every filesystem operations, you can also remove a file or folder in Deno.
Deno provides the remove()
and removeSync()
methods for this task.
remove(path)
removeSync(path)
The following example removes the intro.txt file with Deno.
Deno Removing a File or Folder with remove() or removeSync()
Deno Example Remove a File With remove()
const file_path = 'intro.txt'
// Deno remove file asynchronous (non-block)
await Deno.remove(file_path);
// Deno remove file synchronous (blocking way)
Deno.removeSync(file_path);
Example Explained
- The
file_path
constant stores the string which is the path to the file. - The
remove()
asynchronously removes the file from the path. - The
removeSync()
synchronously removes the file from the path.
Deno Removing a Folder Recursively
The optional RemoveOptions
object is provided to remove a non-empty folder or remove a folder recursively. The recursive
property of the RemoveOptions
objects take care of the job. Let's check an example, which removes a non-empty folder recursively.
Deno Example Remove a File With remove() Recursively
const file_path = 'intro.txt'
// Deno remove file and folder recursively asynchronous (non-block)
await Deno.remove(file_path,{"recursive": true});
// Deno remove file and folder recursively synchronous (blocking way)
Deno.removeSync(file_path,{"recursive": true}));
What You Should Know at The End of This Chapter
- You should be able to read a file in a blocking and non-blocking way with Deno.
- You should be able to write and append a file in Deno.
- You should be able to remove a file in Deno.