node. js File System
node. JS provides a set of UNIX-like (POSIX)-compliant file manipulation APIs. The Node import file System module (FS) syntax is as follows:
var=require("FS")
Asynchronous and synchronous
The methods in the node. js file System (FS module) module have asynchronous and synchronous versions, such as functions that read the contents of the file with asynchronous Fs.readfile () and synchronous Fs.readfilesync ().
The last parameter of the Async method function is a callback function, and the first parameter of the callback function contains the error message (ERR).
We recommend that you use asynchronous methods, which are more efficient, faster, and not blocking than synchronous.
Instance
Create a Input.txt file with the following content:
Novice Tutorial Official website address: www.runoob.com file Read instance
Create the File.js file with the following code:
var fs = require ("FS");//asynchronously reads Fs.readfile (' Input.txt ', function (err, data) { if (err) { return Console.error (err ); } Console.log ("asynchronous reads:" + data.tostring ());}); /synchronous Read var data = Fs.readfilesync (' input.txt '); Console.log ("synchronous read:" + data.tostring ()); Console.log ("program execution is complete. ");
The result of the above code execution is as follows:
$ node File.js synchronous read: Novice Tutorial official website address: www.runoob.com file read instance program execution completed. Asynchronous read: Novice Tutorial official website address: www.runoob.com file Read instance
Excerpt from: http://www.runoob.com/nodejs/nodejs-fs.html
node. js File System