How To Read and Write Files In Deno
Nov 18, 2020
This post will show you how to read a file in Deno as well as how to write a file in Deno.
Reading Files In Deno
Reading files in Deno can be very easy, especially if all you need to do is access the contents as a string. Because we have "top level await" or the ability to await in the root of a file, we just need to await a Deno.readTextFile. Check it out. Where "filename" is a string path to a file.
const filename = "./text.txt"; const file = await Deno.readTextFile(filename)
Now when we run our our file, we need to make sure we use
deno --allowRead yourFile.ts
Writing Files in Deno
Writing text files in Deno is also a one liner.
const stringToBeWritten = "Hello from fileName.txt" const file = await Deno.writeTextFile("fileName.txt", stringToBeWritten)
This time we need to make sure you're using
deno --allowWrite yourFile.ts