Skip to main content

Read and Write in a local file with Deno

this blog, we will see how we can read and write into a local file with the help of Deno

  • First, we will create a program which will read multiple quotes from a text file and then print a random quote on our screen
  • Then we will take input from the user in the command line and write those inputs in a text file

Before You Get Started

This tutorial assumes you have basic knowledge about Deno and you have latest Deno version installed on your system, if you haven't installed Deno on your system or never ran Deno code please look at this great introductory blog - Hello world with Deno

Part 1: Read a file and print text out of it

First, add a file called quotes.txt in the folder you want to run the Deno code, We will read these quotes with the help of Deno

Don't worry about what anybody else is going to do. The best way to predict the future is to invent it. - Alan Kay
Premature optimization is the root of all evil in programming. - Donald Knuth
Clarity and brevity sometimes are at odds. When they are, I choose clarity.  - Jacob Kaplan-Moss
Optimism is an occupational hazard of programming; feedback is the treatment. - Kent Beck
A lot of times, people don't know what they want until you show it to them.  - Steve Jobs
Nine people can't make a baby in a month. - Fred Brooks
A clever person solves a problem. A wise person avoids it. - Albert Einstein

And then create an index.ts file in which we will write Deno code, we can also write Deno code in javascript instead of Typescript, but as Deno is built on TypeScript we are using Typescript here

const getQuotes = async (fileName: string): Promise<Array<string>> => {
 const decoder = new TextDecoder("utf-8");

 const text: string = decoder.decode(await Deno.readFile(fileName));
 return text.split("\n");
};

const quotesArr: Array<string> = await getQuotes("quotes.txt");

const randomQuote: string =  quotesArr[Math.floor(Math.random() * quotesArr.length)];
console.log(randomQuote);

In the getQuotes function, we are using Deno.readFile to read the contents of a file as an array of bytes and using TextDecoder to convert the bytes to string and then splitting it in a string array and return

Then we are just picking a randomQuote with Math.random and console.log that on our screen

To run the above code we need to put --allow-read flag in our command as shown below because we need to access the local filesystem to read the file.

deno run --allow-read index.ts

Deno is secure by default, with no file, network, or environment access unless explicitly enabled. You can run deno run --help to see all the available flags for different permissions

Once your program run initially you will see that Deno is compiling the index.ts file and printing a random quote from quotes.txt, if you run the programme again without changing the code it will not compile it again and will just show another random quote.

deno run --allow-read index.ts
Compile file:///C:/Users/PuneetSingh/Documents/deno_fs/index.ts
Premature optimization is the root of all evil in programming. - Donald Knuth


deno run --allow-read index.ts
A lot of times, people don't know what they want until you show it to them.  - Steve Jobs

Part 2: Create a file and write text in it

Let's create another TypeScript file write.ts and put the below code in it

import { readLines } from "https://deno.land/std@v0.52.0/io/bufio.ts";

console.log('Start typing...');

const encoder = new TextEncoder();
await Deno.writeFile("input.txt", new Uint8Array());

// Listen to stdin input by readLines
for await(const line of readLines(Deno.stdin)) {
   const data = encoder.encode(line+"\n");
   await Deno.writeFile("input.txt", data, {append: true});
   console.log("Wrote the above text in input.txt\n") 
}

In the above code we are using readLines to raed user input line by line, Every time we run the code Deno.writeFile("input.txt", new Uint8Array()) will create an empty input.txt file in the folder.

Then const line of readLines(Deno.stdin) in a loop will wait for the user to input any text, as we will give any input Deno.writeFile with {append: true} is used to append data in input.txt

Let's run the program, Once we run it, whatever text we will give to the program through standard input will be written to input.txt

deno run --allow-write write.ts

Start typing...
This is a demo for Deno
Wrote the above text in input.txt

In this demo, we will write some text in a file
Wrote the above text in input.txt

If you check the input.txt file you will found the content which was given as input

This is a demo for Deno
In this demo, we will write some text in a file

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...