File Handling
The Node.js file system handle file in a smart way. Below is the operation that Node.js can handle on file.
var fs = require("fs")
Create Files
You can Create a new file using appendFile,open,
Below example show how to create a new file using the appendFile() method.
Example
var fs = require('fs');
fs.appendFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Output :'Your file created and content written!'
You have noticed that a new file created 'myTest.txt' , the file content
a string 'Hello World!'.
Below example show how to create a new file using the open() method.
.open() method takes an extra argument for file opening mode.Here are some of those argument.
r:Open for reading.
r+:Open for reading and writing.
rs:Open for reading in synchronous mode.
rs+:Open for reading and writing in synchronous mode.
w:Open for writing.
w+:Open for reading and writing.
Example
var fs = require('fs');
var path = "C:\\Users\\myTest.txt";
fs.open(path, "w+", function(error, fd) {
if (error) {
console.error("open error: " + error.message);
} else {
console.log("Successfully opened " + path);
}
});
Output :'Successfully opened C:\\Users\\myTest.txt'
Below example show how to create a new file using the writeFile() method.
Example
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Output :'Your file created and content written!'
Open Files
You can open file by "open" method.
Method Signature: fs.open(path, flags[, mode], callback)
Parameters:
path: file path with name.
Flag: The flag to perform operation
Mode: The mode for read, write or read write.
Here are some of common Mode for file opening.
r:Open for reading.
r+:Open for reading and writing.
rs:Open for reading in synchronous mode.
rs+:Open for reading and writing in synchronous mode.
w:Open for writing.
w+:Open for reading and writing.
Example
var fs = require('fs');
var path = "C:\\Users\\myTest.txt";
fs.open(path, "w+", function(error, fd) {
if (error) {
console.error("open error: " + error.message);
} else {
console.log("Successfully opened " + path);
}
});
Output :'Successfully opened C:\\Users\\myTest.txt'
Read Files
You can read file from your computer using
Example
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
console.log(data.toString());
});
Output :
Hello World
Writing Files
You can write data to a file from your computer using
Example
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Output :'Your file created and content written!'
Append Files
You can append data to a file from your computer using
Example
var fs = require("fs");
fs.appendFile('input.txt','Hello World!', function (err, data) {
console.log("Your string appended sucessfully");
});
Output :'Your string appended sucessfully'
Synchronous and Asynchronous
Node file system module content several methods. Each methods can be of two type , synchronous and asynchronous. A method is called and the control is move to the next method call, after call, do not wait for response . The architecture of Node.js is context switching, so that it can handle a large number of processing in a single thread. The synchronous and asynchronous, both method are available in the Nodejs File System module. The difference between synchronous and asynchronous is that of last parameter. An asynchronous method accept an extra parameter call back function as error. Below is example of synchronous and asynchronous method.
Asynchronous method
Example 1
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
console.log(data.toString());
});
Example2
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Synchronous method
Example 1
var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
Example 2
var fs = require("fs");
var data = fs.writeFileSync('C:\myTest.txt', 'Hello World');
console.log("file written sucessfully");
The Node.js file system handle file in a smart way. Below is the operation that Node.js can handle on file.
- Create File
- Open File
- Read File
- Writing File
- Append File
- Delete File
- Rename File
- Upload File
- Truncate File
- Copy File
var fs = require("fs")
Create Files
You can Create a new file using appendFile,open,
writeFile
method.Below example show how to create a new file using the appendFile() method.
Example
var fs = require('fs');
fs.appendFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Output :'Your file created and content written!'
You have noticed that a new file created 'myTest.txt' , the file content
a string 'Hello World!'.
Below example show how to create a new file using the open() method.
.open() method takes an extra argument for file opening mode.Here are some of those argument.
r:Open for reading.
r+:Open for reading and writing.
rs:Open for reading in synchronous mode.
rs+:Open for reading and writing in synchronous mode.
w:Open for writing.
w+:Open for reading and writing.
Example
var fs = require('fs');
var path = "C:\\Users\\myTest.txt";
fs.open(path, "w+", function(error, fd) {
if (error) {
console.error("open error: " + error.message);
} else {
console.log("Successfully opened " + path);
}
});
Output :'Successfully opened C:\\Users\\myTest.txt'
Below example show how to create a new file using the writeFile() method.
Example
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Output :'Your file created and content written!'
Open Files
You can open file by "open" method.
Method Signature: fs.open(path, flags[, mode], callback)
Parameters:
path: file path with name.
Flag: The flag to perform operation
Mode: The mode for read, write or read write.
Here are some of common Mode for file opening.
r:Open for reading.
r+:Open for reading and writing.
rs:Open for reading in synchronous mode.
rs+:Open for reading and writing in synchronous mode.
w:Open for writing.
w+:Open for reading and writing.
Example
var fs = require('fs');
var path = "C:\\Users\\myTest.txt";
fs.open(path, "w+", function(error, fd) {
if (error) {
console.error("open error: " + error.message);
} else {
console.log("Successfully opened " + path);
}
});
Output :'Successfully opened C:\\Users\\myTest.txt'
Read Files
You can read file from your computer using
fs.readFile()
method.Example
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
console.log(data.toString());
});
Output :
Hello World
Writing Files
You can write data to a file from your computer using
fs.readFile()
method.If file already exists then it overwrites the existing content otherwise it creates a new file and writes data into it. Example
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Output :'Your file created and content written!'
Append Files
You can append data to a file from your computer using
fs.
appendFile
()
method.Example
var fs = require("fs");
fs.appendFile('input.txt','Hello World!', function (err, data) {
console.log("Your string appended sucessfully");
});
Output :'Your string appended sucessfully'
Synchronous and Asynchronous
Node file system module content several methods. Each methods can be of two type , synchronous and asynchronous. A method is called and the control is move to the next method call, after call, do not wait for response . The architecture of Node.js is context switching, so that it can handle a large number of processing in a single thread. The synchronous and asynchronous, both method are available in the Nodejs File System module. The difference between synchronous and asynchronous is that of last parameter. An asynchronous method accept an extra parameter call back function as error. Below is example of synchronous and asynchronous method.
Asynchronous method
Example 1
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
console.log(data.toString());
});
Example2
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
console.log('Your file created and content written!');
});
Synchronous method
Example 1
var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
Example 2
var fs = require("fs");
var data = fs.writeFileSync('C:\myTest.txt', 'Hello World');
console.log("file written sucessfully");
No comments:
Post a Comment