Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js JavaScript Learn
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
Node.js console.table()
Node.js DNS Code
Node.js cipher.final()
Node.js URL.hash API
Node.js URL.host API
Node.js URL.origin API
Node.js MySQL
Folders in Node.js
Folders Node.js part3
Rename Folder Node.js
Remove Folder Node.js
 


  

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

Home >> 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:-


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.



Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |