You are on page 1of 6

NAME: SHUBHAM MISHA

ROLL NO: 34
2ND SHIFT

PRACTICAL-5

Aim: Using File Handling demonstrate all basic file operations (Create, write, read, delete)

Theory:

 The Node.js file system module allows you to work with the file system on your
computer.

 To include the File System module, use the require() method:

var fs = require('fs');

o Common use for the File System module:

 Read files
 Create files
 Write files
 Delete files

The fs.readFile() method is used to read files on your computer.

The File System module has methods for creating new files:

 fs.appendFile()
 fs.open()
 fs.writeFile()

The fs.appendFile() method appends specified content to a file.

To delete a file with the File System module, use the fs.unlink() method.

Node.js is a server-side environment for executing JavaScript files. Off the browser, Node.js
offers even more features to complement JavaScript functionalities. Node.js is built around
adding functionalities that cannot be performed or utilized in a browser. In this article, we will
cover file handling extensively, understand the various functionalities Node.js offers, and
implement code to read and write to files.

Let us look at some of use cases.

1. Logging: While creating scalable systems, feedback is an important part of maintaining those
systems(and sub-systems). Sub-systems generate log files that are read by other sub-systems
and then take action based on the feedback given. All this is made possible by reading and
writing into files.
2. System maintenance: Lets consider an example of a Google servers system’s engineer. The
current status of all the operations are communicated to the engineer via a detailed report
given a suitable format.
3. Temporary files: Many times when we run programs on servers, temporary files are created
to store some temporary but important information. These temporary files can store data such
as event IDs, cookies, etc. This is done to reduce the load on the main memory and balance
the load between the main and secondary memory devices.

TO READ FILE():

READ.JS

Output:
READ.TXT

To write a file()

Write.js

Output:
Write.txt

Creating a file()

Create.js

Output:
Input.txt

Deleting a file()

Following is a list of files.

Delete.txt
Following is a list of file after deleting a “text3.txt”

You might also like