require(‘fs);
The table below gives the list of File System methods used in Node.js.
Methods | Description |
---|---|
fs.appendFile(file, data[, options], callback) | The existing content is added with the new content. |
fs.access(path[, mode], callback) | The user permissions are tested for a particular file. |
fs.exists(path, callback) | The method check whether the specified file exist or not. |
fs.utimes(path, atime, mtime, callback) | The method changes the file timestamp. |
fs.readdir(path, callback) | The specified directory content can be read by using this method. |
fs.mkdir(path[, mode], callback) | The method is used to create a new directory. |
fs.symlink(destination, path[, type], callback) | The method asynchronously symlink. |
fs.link(srcpath, dstpath, callback) | The file is linked asynchronously using this method. |
fs.stat(path, callback) | The method returns the object fs.stat including with the important statistics of the file. |
fs.chown(path, uid, gid, callback) | The method asynchronously chown. |
fs.rename(oldPath, newPath, callback) | The existing file can be renamed using this method. |
fs.open(path, flags[, mode], callback) | The method is used to open a file for reading/writing. |
fs.writeFile(filename, data[, options], callback) | The method creates a new file and write the data into the file, in case if the file exists then overwrite the content. |
fs.readFile(fileName [,options], callback) | The method is used to read the existing file. |
fs.writeFile(filename, data[, options], callback)
A new file is created and data is written into the file using the write function or else if the file is created already then the function overwrites the data in that file. The function contain four arguments as follows.
fs.appendFile()
The snippet code below is used to add the content to the above created file i.e. demo.txt.
append.js
[c]
var fs = require('fs');
fs.appendFile('demo.txt', '-Tutorials', function (err) {
if (err)
console.log(err);
else
console.log('Extra content is added to the creted file.');
});
[/c]
Compiling the above code will produce the output as shown in the image below.
Now, check the file demo.txt in the working directory the data in the file get added with the given content as shown in the image below.
fs.readFile(fileName [,options], callback)
A new file is created and data is written into the file or else if the file is created already then the function overwrites the data in that file. The function contain four arguments as follows.
The data from a file can be read using the read function either in a synchronously or asynchronously. The function have three arguments as follows.
fs.open(path, flags[, mode], callback)
The function have four arguments as follows.
Flags List | Description |
---|---|
a | The flag opens the file for appending and in case when a file does not exist a new file is created. |
ax | The flag is similar to flag 'a' but fails if the path exists. |
a+ | The flag opens the file for reading and appending, in case when a file does not exist a new file is created. |
ax+ | The flag is similar to flag 'a+' but fails if the path exists. |
w | The flag opens the file for writing, in case when a file does not exist a new file is created or if exist the file get truncated. |
wx | The flag is similar to flag 'w' but fails if the path exists. |
w+ | The flag opens the file for reading and writing, in case when a file does not exist a new file is created or if exist the file get truncated. |
wx+ | The flag is similar to flag 'wx+' but fails if the path exists. |
r | The flag opens the file for reading, in case when a file does not exist an exception get occurs. |
r+ | The flag opens the file for reading and writing, in case when a file does not exist an exception get occurs. |
rs | The flag opens the file in synchronous mode for reading. |
rs+ | The flag opens the file for reading and writing and tell the OS to open the file in synchronous mode. |
fs.unlink(path, callback);
The function have tow arguments as follows.