[Go to site: main page, start]

0% found this document useful (0 votes)
9 views29 pages

Node.js File System Module Overview

pes web tech Nodejs File System Module

Uploaded by

Vinay Msd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views29 pages

Node.js File System Module Overview

pes web tech Nodejs File System Module

Uploaded by

Vinay Msd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WEB TECHNOLOGIES

[Link]: File System Module

Prof. Pavan A C
Department of Computer Science and Engineering

Acknowledgement:
Teaching Assistants(Samved Suresh Jain and Sathyajit
P)
NODE JS
Fs Module

● fs stands for File System.

● It is a core module in [Link] (you don’t need to install it


separately, just require('fs')).

● It allows you to interact with the file system on your computer,


meaning you can create, read, write, update, delete, and manage
files and directories directly from your [Link] program.

● Since [Link] is often used for backend development, fs is


essential for working with configuration files, logs, user uploads, or
any data stored on disk.
NODE JS
Fs module operations

Common use for the File System module:


• Read files
• Create files
• Update files
• Delete files
• Rename files
NODE JS
Fs module operations

[Link]() → Checks if a file exists and [Link] has permission to access it.

[Link]() → Appends data to a file, creating it if it doesn’t exist.

[Link]() → Changes the permissions of a file.

[Link]() → Changes the owner and group of a file.

[Link]() → Closes an open file descriptor.

[Link]() → Copies the content of one file to another.

[Link]() → Creates a readable stream to read file data in chunks.

[Link]() → Creates a writable stream to write data to a file in chunks.


NODE JS
Fs module operations

[Link]() → Creates a new hard link to an existing file.

[Link]() → Creates a new directory (folder).

[Link]() → Creates a unique temporary directory.

[Link]() → Opens a file and sets its mode (read, write, etc.).

[Link]() → Reads the contents of a directory.

[Link]() → Reads the entire content of a file into memory.

[Link]() → Reads the value (target) of a symbolic link.

[Link]() → Resolves relative paths (., ..) to absolute paths.


NODE JS
Fs module operations

[Link]() → Renames or moves a file/folder.

[Link]() → Removes (deletes) a directory.

[Link]() → Returns information (size, type, timestamps) about a file.

[Link]() → Creates a new symbolic (soft) link to a file.

[Link]() → Shrinks or extends a file to a specified length.

[Link]() → Deletes a file or symbolic link.

[Link]() → Stops watching a file for changes.

[Link]() → Updates the access and modification times of a file.

[Link]() → Starts watching a file for changes (polling).

[Link]() → Writes data to a file, replacing it if it exists.


NODE JS
Sync and Async Methods

The fs module provides two styles of methods:


1. Synchronous Methods ([Link], [Link], etc.)
○ Block the execution of code until the operation finishes.
○ Easier for small scripts but not recommended for large
applications (since they block the event loop).
2. Asynchronous Methods ([Link], [Link], etc.)
○ Non-blocking (execution continues while file operation happens in
the background).
○ Use callbacks or promises (with [Link]) to handle results.
Recommended for production apps.
NODE JS
Fs File Open

The [Link]() method is used to create, read, or write a file. The


[Link]() method is only for reading the file and [Link]() method
is only for writing to the file, whereas [Link]() method does several
operations on a file. First, we need to load the fs class which is a module
to access the physical file system.
Syntax:
[Link](path, flags, mode, callback)
NODE JS
Fs File Open

Syntax
Following is the syntax of the method to open a file in asynchronous mode −
[Link](path, flags[, mode], callback)

Parameters
Here is the description of the parameters used −
path − This is the string having filename including path.
flags − Flags indicate the behavior of the file to be opened. All possible values
have been mentioned below.
mode − It sets the file mode (permission and sticky bits), but only if the file was
created. It defaults to 0666, readable and writable.
callback − This is the callback function which gets two arguments (err, fd).
Flags for read/write operations are − r,r+,rs,rs+,w,wx,w+,wx+,a,ax,a+,ax+
NODE JS
Fs File Open Modes

Flag Mode If File Exists If File Doesn’t Exist Use Case

r Read OK Error Reading only

r+ Read + Write OK Error Read & write without creating

rs Read (sync) OK Error Direct disk read

rs+ Read + Write (sync) OK Error Direct disk read/write

w Write Truncate (empty) Create new Overwrite/create file

wx Write (exclusive) Error Create new Safe create (no overwrite)

w+ Read + Write Truncate (empty) Create new Read & overwrite/create

wx+ Read + Write (ex) Error Create new Safe read/write create

a Append Append Create new Add data at end

ax Append (exclusive) Error Create new Safe append create

a+ Read + Append Append Create new Read & add data

ax+ Read + Append (ex) Error Create new Safe read+append create
NODE JS
Fs - File Open Async

If there is no file present, a new file is created.


NODE JS
Fs - File Open Sync

If there is no file present, a new file is created.


NODE JS
Fs File Read

Syntax
[Link](fd, buffer, offset, length, position, callback) This method will use file
descriptor to read the file. If you want to read the file directly using the file
name, then you should use another method available.

Parameters
fd − This is the file descriptor returned by [Link]().
buffer − This is the buffer that the data will be written to.
offset − This is the offset in the buffer to start writing at.
length − This is an integer specifying the number of bytes to read.
position − This is an integer specifying where to begin reading from in the file.
If position is null, data will be read from the current file position.
callback − This is the callback function which gets the three arguments, (err,
bytesRead, buffer).
NODE JS
Fs File Read

The file you want to read must already be present else an error
occurs.
NODE JS
Fs File Read

The file you want to read must already be present else an error
occurs.
NODE JS
Fs File Write

Syntax
[Link](filename, data[, options], callback)
This method will over-write the file if the file already exists. If you want to write into
an existing file then you should use another method available.

Parameters
path − This is the string having the file name including path.
data − This is the String or Buffer to be written into the file.
options − The third parameter is an object which will hold {encoding, mode, flag}. By
default. encoding is utf8, mode is octal value 0666. and flag is 'w'
callback − This is the callback function which gets a single parameter err that returns
an error in case of any writing error.
NODE JS
Fs File Open and Write

Before open and write operation:


NODE JS
Fs File Open and Write

After open and write operation:


NODE JS
Fs File Write

Before writing text:


NODE JS
Fs File Write

Code used to write content:


NODE JS
Fs File Write

After writing text:


NODE JS
Fs File Append

Syntax: [Link](path, data[, options], callback)


This method appends data to a file.

● If the file does not exist → it creates a new file and writes the data.
● If the file already exists → it adds the data at the end (instead of overwriting like
writeFile).

Parameters

1. path - String containing the file name (and optionally the path).
2. data - The content to append (String or Buffer).
3. options (optional) - An object or string controlling file behavior:
encoding → default is "utf8".
mode → default is 0o666 (octal), defines permissions.
flag → default is "a" (append mode).
4. callback - Function with one parameter: err. Called once the append operation is
finished. If an error occurs, err will contain details.
NODE JS
Fs File Append

Before appending new content to file:


NODE JS
Fs File Append

Code used for appending new content to file:


NODE JS
Fs File Append

After appending new content to file:


NODE JS
Fs File Close,Delete,Truncate
Unlinking a File
Use [Link]() method to delete an existing file.
[Link](path, callback);
● path − This is the file name including path.
• callback − This is the callback function No arguments other than a
possible exception are given to the completion callback.
Closing a File
[Link](fd, callback)
fd − This is the file descriptor returned by file [Link]() method.
callback − This is the callback function No arguments other than a possible exception are given to
the completion callback.
Truncate a File
[Link](fd, len, callback)
fd − This is the file descriptor returned by [Link]().
len − This is the length of the file after which the file will be truncated.
callback − This is the callback function No arguments other than a possible exception are given to
the completion callback.
NODE JS
Fs File delete

After running this code the file [Link] is deleted.


NODE JS
Fs Module – Other Important Methods
THANK YOU

Prof. Pavan A C
Department of Computer Science and Engineering

You might also like