fs.appendFile(path,
data[, options], callback)This method appends data to a file synchronously. Below
table summarizes the arguments of appendFile method.
Argument
|
Type
|
Description
|
||||||||||||
path
|
string
or Buffer or URL or number
|
Specifies
the file name or file descriptor.
|
||||||||||||
data
|
string
or Buffer
|
Data
to be appended to a file
|
||||||||||||
options
|
string
or Object
|
Below
table summarizes the options supported by appendFile method.
|
||||||||||||
callback
|
Function
|
This
function is called, when the data is appended to the file. This function
takes ‘err’ as an argument. If any error occurs while appending the content,
‘err’ is populated with error information.
|
Below
table summarizes the modes supported by node.js
Constant (mode)
|
Octal value
|
Description
|
fs.constants.S_IRUSR
|
0o400
|
read
by owner
|
fs.constants.S_IWUSR
|
0o200
|
write
by owner
|
fs.constants.S_IXUSR
|
0o100
|
execute/search
by owner
|
fs.constants.S_IRGRP
|
0o40
|
read
by group
|
fs.constants.S_IWGRP
|
0o20
|
write
by group
|
fs.constants.S_IXGRP
|
0o10
|
execute/search
by group
|
fs.constants.S_IROTH
|
0o4
|
read
by others
|
fs.constants.S_IWOTH
|
0o2
|
write
by others
|
fs.constants.S_IXOTH
|
0o1
|
execute/search
by others
|
Find
the below working application.
var fs = require('fs'); var filePath = 'out.txt'; var msg1 = 'Hello, How are you\n'; var msg2 = 'I am fine, How are you?\n'; var msg3 = 'I am fine too, thank you!!!!!\n'; fs.appendFile(filePath, msg1, (err) => { if (err) { console.log('error occured while appending %s', msg1); } }); fs.appendFile(filePath, msg2, (err) => { if (err) { console.log('error occured while appending %s', msg1); } }); fs.appendFile(filePath, msg3, (err) => { if (err) { console.log('error occured while appending %s', msg1); } });
Open
‘out.txt’, you can see below messages.
I
am fine, How are you?
I
am fine too, thank you!!!!!
Hello,
How are you
I
am fine too, thank you!!!!!
I
am fine, How are you?
Hello,
How are you
No comments:
Post a Comment