home PYTHONJAVA
 

Node.js File System (FS)


In Node.js, file I/O is given by basic wrappers around standard POSIX functions. Hub File System (fs) module can be imported utilizing following sentence structure:

Syntax:

  1. var fs = require("fs")  

Node.js FS Reading File

Every strategy in fs module has synchronous and offbeat structures.

Asynchronous techniques accept a last parameter as consummation work callback. Nonconcurrent technique is favored over synchronous strategy since it never obstructs the program execution where as the synchronous strategy blocks.

Let's take an example:

Create a content document named "input.txt" having the accompanying content.

File: input.txt

  1. welookups is a one of the best online tutorial website to learn different technologies  
  2.  in a very easy and efficient manner.  

Let's take a guide to make a JavaScript file named "main.js" having the accompanying code:

File: main.js

  1. var fs = require("fs");  
  2. // Asynchronous read  
  3. fs.readFile('input.txt', function (err, data) {  
  4.    if (err) {  
  5.        return console.error(err);  
  6.    }  
  7.    console.log("Asynchronous read: " + data.toString());  
  8. });  
  9. // Synchronous read  
  10. var data = fs.readFileSync('input.txt');  
  11. console.log("Synchronous read: " + data.toString());  
  12. console.log("Program Ended");  

Open Node.js command prompt and run the main.js:

  1. node main.js  

Node.js Open a file

Syntax:

Following is the sentence structure of the technique to open a document in nonconcurrent mode:

  1. fs.open(path, flags[, mode], callback)   

Parameter explanation:

Following is the portrayal of parameters utilized in the above syntax:

path: This is a string having document name including path.

flags: Flag determines the conduct of the file to be opened. Every conceivable esteem have been referenced below.

mode: This sets the document mode (authorization and sticky bits), yet just if the file was made. It defaults to 0666, meaningful and writeable.

callback: This is the callback work which gets two contentions (fail, fd).

Node.js Flags for Read/Write

p>Following is a rundown of banners for read/compose operation:

Flag Description
r open file for reading. a special case happens if the file does not exist.
r+ open file for reading and composing. a special case happens if the file does not exist.
rs open file for reading in synchronous mode.
rs+ open file for reading and composing, advising the os to open it synchronously. see notes for 'rs' about utilizing this with caution.
w open document for composing. the file is made (in the event that it doesn't exist) or truncated (on the off chance that it exists).
wx like 'w' however falls flat if way exists.
w+ open document for reading and composing. the file is made (in the event that it doesn't exist) or truncated (on the off chance that it exists).
wx+ like 'w+' however comes up short if way exists.
a open file for annexing. the file is made whether it doesn't exist.
ax like 'a' yet falls flat if way exists.
a+ open file for reading and affixing. the document is made whether it doesn't exist.
ax+ open file for reading and attaching. the document is made whether it doesn't exist.

Create a JavaScript file named "main.js" having the following code to open a file input.txt for reading and writing.

File: main.js

  1. var fs = require("fs");  
  2. // Asynchronous - Opening File  
  3. console.log("Going to open file!");  
  4. fs.open('input.txt', 'r+', function(err, fd) {  
  5.    if (err) {  
  6.        return console.error(err);  
  7.    }  
  8.   console.log("File opened successfully!");       
  9. });  

Node.js fs.Stats class Methods

Method Description
stats.isfile() returns true if file type of a simple file.
stats.isdirectory() returns true if file type of a directory.
stats.isblockdevice() returns true if file type of a block device.
stats.ischaracterdevice() returns true if file type of a character device.
stats.issymboliclink() returns true if file type of a symbolic link.
stats.isfifo() returns true if file type of a fifo.
stats.issocket() returns true if file type of asocket.





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.