190 likes | 228 Views
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.<br>
E N D
Node File System iFour Consultancy https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Introduction • 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 = require('fs'); https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Use of File System • Common use for the File System module: • Read files • Write files • Append files • Delete files • Rename files https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Synchronous vs Asynchronous • 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. https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Node.js Reading File • 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 https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Node.js Reading File • Example: Create File main.js For Asynchronous read : var fs = require("fs"); // Asynchronous read fs.readFile('input.txt', function (err, data) { if (err) { return console.error(err); } console.log("Asynchronous read: " + data.toString()); https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Node.js Reading File • Example: Create File main.js For Synchronous read : var data = fs.readFileSync('input.html'); console.log("Synchronous read: " + data.toString()); console.log("Program Ended"); • Open command prompt and run the main.js: > node main.js https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Node.js Writing a file • 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.writeFile(filename, data[, options], callback) https://www.ifourtechnolab.com/nodejs-blockchain-software-development
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 = require('fs'); var content= "this is the content in the file"; fs.writeFile('input.txt', content , (err) => { if (err) throw err; console.log('It\'s saved!'); }); https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Node.js Writing a file • Example: Create File main.js For Synchronous write : var fs = require('fs'); var content = "We are writing this file synchronously using node.js"; fs.writeFileSync('input.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
Append a File using Nodejs • 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.appendFile(filepath, data, options, callback_function); fs.appendFileSync(filepath, data, options); https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Append a File using Nodejs • There are two ways for Appending a file using nodejs : • Example: Create File main.js For Asynchronous Append : var fs = require('fs'); new_data = "This data will be appended at the end of the file."; fs.appendFile('input.txt', new_data , (err) => { if(err) throw err; console.log('The new_content was appended successfully'); }); https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Append a File using Nodejs • Example: Create File main.js For Synchronous Append : var fs = require('fs'); var content = "We are Appending this file synchronously using node.js"; fs.appendFileSync('input.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
Rename a File in Nodejs • To rename file with Node FS, use fs.rename(new_file_name, old_file_name, callback_function) for asynchronous file rename operation and use fs.renameSync(new_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.rename(new_file_path, old_file_path, callback_function) fs.renameSync(new_file_path, old_file_path) https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Rename a File in Nodejs • There are two ways for Appending a file using nodejs : • Example: Create File main.js For Asynchronous Rename : var fs = require('fs'); fs.rename('input.txt', 'newinput.txt', (err) => { if (err) throw err; console.log('File renamed successfully'); }); console.log("This method is Asynchronous"); https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Rename a File in Nodejs • Example: Create File main.js For Synchronous Rename : var fs = require('fs'); fs.renameSync('input.txt', 'newinput.txt'); console.log('File renamed successfully'); console.log("This method is Synchronous"); • Open command prompt and run the main.js: > node main.js https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Delete a File in Nodejs • To delete a file in Node.js, Node FS unlink(path, callback) can be used for asynchronous file operation and unlinkSync(path) can be used for synchronous file operation. • Syntax: fs.unlink(filePath, callbackFunction) fs.unlinkSync(filePath) https://www.ifourtechnolab.com/nodejs-blockchain-software-development
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 = require('fs'); var filename = 'input.txt'; fs.unlink(filename, (err) => { if (err) throw err; console.log('File deleted successfully'); }); https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Delete a File in Nodejs • Example: Create File main.js For Synchronous Delete: var fs = require('fs'); var filename = 'input.txt'; fs.unlinkSync(filename); console.log('File Deleted Successfully'); • Open command prompt and run the main.js: > node main.js https://www.ifourtechnolab.com/nodejs-blockchain-software-development