Tutorial on Node File System

Views:
 
     
 

Presentation Description

The Node.js file system module allows you to work with the file system on your computer. Node.js gives the functionality of File I/O by providing wrappers around the standard POSIX functions. In Node.js, File I/O methods can be performed in both synchronous as well as asynchronous form depending upon the user requirements.

Comments

Presentation Transcript

slide 1:

iFour Consultancy Node File System https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 2:

 Node.js gives the functionality of File I/O by providing wrappers around the standard POSIX functions. In Node.js File I/O methods can be performed in both synchronous as well as asynchronous form depending upon the user requirements.  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 requirefs Introduction https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 3:

 Common use for the File System module: 1. Read files 2. Write files 3. Append files 4. Delete files 5. Rename files Use of File System https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 4:

 Every method in the fs module has synchronous as well as asynchronous forms. Asynchronous methods take the last parameter as the completion function callback and the first parameter of the callback function as error. It is better to use an asynchronous method instead of a synchronous method as the former never blocks a program during its execution whereas the second one does. Synchronous vs Asynchronous https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 5:

 Every method in fs module has synchronous and asynchronous forms.  Asynchronous methods take a last parameter as completion function callback. Asynchronous method is preferred over synchronous method because it never blocks the program execution where as the synchronous method blocks.  Example: Create File: input.txt We are reading this file using node.js Node.js Reading File https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 6:

 Example: Create File main.js For Asynchronous read : var fs require"fs" // Asynchronous read fs.readFileinput.txt function err data if err return console.errorerr console.log"Asynchronous read: " + data.toString Node.js Reading File https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 7:

 Example: Create File main.js For Synchronous read : var data fs.readFileSyncinput.html console.log"Synchronous read: " + data.toString console.log"Program Ended"  Open command prompt and run the main.js: node main.js Node.js Reading File https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 8:

 Node.js fs.writeFile function writes data to a file asynchronously with replacing the file in case of already exists. This function can write data from string or a buffer.  The encoding option is ignored if data is a buffer. It defaults encoding is ‘utf8’ Default file mode is 0666 and default flag is used ‘w’ means write mode. 1. path is the filename with path. 2. data is the String or buffer to write 3. options can be an object which is like encoding mode flag. 4. callback function takes single parameter err and used to return errors.  Syntax: fs.writeFilefilename data options callback Node.js Writing a file https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 9:

Node.js Writing a file  There are two ways for writing a file in nodejs :  Example: Create File main.js For Asynchronous Write : var fs requirefs var content "this is the content in the file" fs.writeFileinput.txt content err if err throw err console.logIt\s saved https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 10:

Node.js Writing a file  Example: Create File main.js For Synchronous write : var fs requirefs var content "We are writing this file synchronously using node.js" fs.writeFileSyncinput.txt content console.log"File Written Successfully"  Open command prompt and run the main.js: node main.js https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 11:

 To append data to file in Node.js use Node FS appendFile function for asynchronous file operation or Node FS appendFileSync function for synchronous file operation. filepath is a String that specifies file path data is what you append to the file options to specify encoding/mode/flag  Syntax: fs.appendFilefilepath data options callback_function fs.appendFileSyncfilepath data options Append a File using Nodejs https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 12:

 There are two ways for Appending a file using nodejs :  Example: Create File main.js For Asynchronous Append : var fs requirefs new_data "This data will be appended at the end of the file." fs.appendFileinput.txt new_data err iferr throw err console.logThe new_content was appended successfully Append a File using Nodejs https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 13:

Append a File using Nodejs Example: Create File main.js For Synchronous Append : var fs requirefs var content "We are Appending this file synchronously using node.js" fs.appendFileSyncinput.txt content console.log"File Appended Successfully"  Open command prompt and run the main.js: node main.js https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 14:

 To rename file with Node FS use fs.renamenew_file_name old_file_name callback_function for asynchronous file rename operation and use fs.renameSyncnew_file_name old_file_name for synchronous file rename operation. new_file_path : The new file path you would like to assign old_file_path : Path to the file whose name is to be changed callback_function : When file renaming operation is done Callback Function is called with an error object.  Syntax: fs.renamenew_file_path old_file_path callback_function fs.renameSyncnew_file_path old_file_path Rename a File in Nodejs https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 15:

 There are two ways for Appending a file using nodejs :  Example: Create File main.js For Asynchronous Rename : var fs requirefs fs.renameinput.txt newinput.txt err if err throw err console.logFile renamed successfully console.log"This method is Asynchronous" Rename a File in Nodejs https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 16:

Example: Create File main.js For Synchronous Rename : var fs requirefs fs.renameSyncinput.txt newinput.txt console.logFile renamed successfully console.log"This method is Synchronous"  Open command prompt and run the main.js: node main.js Rename a File in Nodejs https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 17:

 To delete a file in Node.js Node FS unlinkpath callback can be used for asynchronous file operation and unlinkSyncpath can be used for synchronous file operation.  Syntax: fs.unlinkfilePath callbackFunction fs.unlinkSyncfilePath Delete a File in Nodejs https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 18:

Delete a File in Nodejs  There are two ways for Appending a file using nodejs :  Example: Create File main.js For Asynchronous Delete : var fs requirefs var filename input.txt fs.unlinkfilename err if err throw err console.logFile deleted successfully https://www.ifourtechnolab.com/nodejs-blockchain-software-development

slide 19:

Example: Create File main.js For Synchronous Delete: var fs requirefs var filename input.txt fs.unlinkSyncfilename console.logFile Deleted Successfully  Open command prompt and run the main.js: node main.js Delete a File in Nodejs https://www.ifourtechnolab.com/nodejs-blockchain-software-development

authorStream Live Help