crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Node.js File System Part1
Question Posted on 01 Apr 2024Home >> Tutorial >> Node.js Tutorial >> Node.js File System Part1

Node.js File System Part1

Node.js File System (FS)


When we say about the File System in Node.js, file I/O is provided by simple wrappers around standard POSIX functions. And to import the Node FS module you can use below syntax:-

Syntax:
var fs = require("fs")


Node.js File System Reading File


Well when we saya bout the Node.js File System, every method in fs module has synchronous and asynchronous forms.

Asynchronous methods take a last parameter as completion function callback. And asynchronous method is preferred over synchronous method because it never block the program execution where as the synchronous methods blocks.

Now take an example:

Create a text file named input.txt having the following content:


Text File Node.js
Now we will take an example to create a JavaScript file named "main.js" file which is having below code:-

File: main.js


Asynchronous File

Node.js Open a File


Below is the syntax pf the method to open a file in asynchronous mode:-
fs.open(path, flags[, mode], callback)


Parameter explanation:
Below are the description of parameters used in above code syntax:-

  • (1)Path:-This parameter is a string which is having file name which include a path.

  • (2)flags:-This parameter specifies the behavior of the file to be opened. And all of the possible values have been mention below in grid.

  • (3)mode:-This parameter will used to sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666, readable and writeable.

  • (4)callback:-This parameter os the callback function which gets two agruments (err, fd).


Node.js Flags for Read/Write


Below is a list of flags for read or write operation:-
0
0

NoFlagDescription
1rThis will be used to open file for reading and through exception if the file soes not exist.
2r+Here this flag is use to read or write file and this will also produce error if the file does not exist.
3rsWell from this flag file is open for reading in synchronous mode.
4rs+Well this flag is use to read or write on file and this will also tell the OS to open it synchronously.
5wWell this flag is mainly used for writing. and this will crete the file if it is not exist or truncate the file if it is exists.
6wxWell this flag is same as 'w' but fails if path exists.
7w+This flag is used to open file for reading and writing. the file is created (if it does not exist) or truncated (if it exists).
8wx+This will work same as 'w+' but fails of path exists.
9aThis will be used to open file for appending and this will create a file it is does not exist.
10axThis flag will do the same as we do in flag 'a' but this will fails if path exists.
11a+This flag is used to open file for reading and appending. And file is created if it is not exist.
12ax+This flag os used to open file for reading and appedning and this will create a file if it does not exist.


.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

Parts of Node.js

Install Node.js Linux

Node.js First Example

Node.js Globals

Node.js DNS






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap