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.
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 = require('fs');
Introduction
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
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
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
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
6. 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());
Node.js Reading File
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
7. 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
Node.js Reading File
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
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.writeFile(filename, data[, options], callback)
Node.js Writing a file
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
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 = 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
10. 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
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.appendFile(filepath, data, options, callback_function);
fs.appendFileSync(filepath, data, options);
Append a File using Nodejs
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
12. 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');
});
Append a File using Nodejs
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
13. 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
14. 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)
Rename a File in Nodejs
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
15. 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");
Rename a File in Nodejs
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
16. 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
Rename a File in Nodejs
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
17. 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)
Delete a File in Nodejs
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
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 = 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
19. 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
Delete a File in Nodejs
https://www.ifourtechnolab.com/nodejs-blockchain-software-development
Notas del editor
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/
Software Outsourcing Company India - http://www.ifourtechnolab.com/