Node. js file system synchronization and asynchronous synchronization code are executed from top to bottom. Asynchronous is not controlled by space. Case study: file. js? 1.1.23 learning points:
Synchronous and asynchronous
Open a file
Get File Information
Write files
Read files
Close file
Intercepting a file
Delete an object
Create directory
View directory
Delete directory
Node. js File System
Synchronous and asynchronous
Synchronous code is executed from top to bottom, and Asynchronization is not controlled by Space
Case: file. js
[Code] var fs = require ('fs'); // fs.readfile('input.txt ', function (err, data) {if (err) return console. log (err); console. log ('asynchronous read: '+ data. toString ();}) // synchronously read var data = fs.readFileSync('input.txt '); console. log ('synchronous read: '+ data. toString (); console. log ('program execution is complete. ');
Open a file
[Code] fs. open (path, flags [, mode], callback) parameters are described as follows: path-file path. Flags-file opening behavior. Mode-sets the file mode (permission). The default permission for file creation is 0666 (readable and writable ). The callback-callback function has two parameters: callback (err, fd ).
Case: open. js
[Code] var fs = require ('fs'); // asynchronously open the file console. log ('Prepare to open file'); // open inptu.txtfs.open('input.txt ', 'r +', function (err, fd) {if (err) return console. log (err); console. log ('file opened successfully');}); console. log ('program execution completed ');
Get File Information
[Code] fs. start (path, callback) parameters are described as follows: path-file path. The callback-callback function has two parameters: (err, stats), and stats is the fs. Stats object.
Case: info. js
[Code] var fs = require('fs'{lele.log('{open the file '{%fs.stat('input.txt ', function (err, stats) {if (err) return console. error (err); console. log (stats); console. log ('file Information read successfully'); // checks the file type console. log ('Is it a file (isFile )? '+ Stats. isFile (); console. log (' Is it a directory (isDirectory )? '+ Stats. isDirectory ());})
Write files
[Code] fs. writeFile (filename, data [, options], callback) if the file exists, the written content will overwrite the parameters of the old file as follows: path-file path. Data-the data to be written into the file. It can be a String or a Buffer (Stream) object. Options-this parameter is an object that contains {encoding, mode, flag }. The default encoding is utf8, the mode is 0666, and the flag is 'W' callback-callback function. The callback function only contains the error message parameter (err) and returns the result when the write fails.
Case: write. js
[Code] var fs = require('fs'{}le.log('{write into the file '{}fs.writefile('input.txt ',' I am the newly written content', function (err) {if (err) console. error (err); console. log ('data written data'); Comment ', function (err, data) {if (err) console. error (err); console. log ('asynchronously read file data: '+ data. toString ());})
Read files
[Code] fs. writeFile (filename, data [, options], callback) if the file exists, the content written in this method will overwrite the content of the old file. The parameters are described as follows: path-file path. Data-the data to be written into the file. It can be a String or a Buffer (Stream) object. Options-this parameter is an object that contains {encoding, mode, flag }. The default encoding is utf8, the mode is 0666, and the flag is 'W' callback-callback function. The callback function only contains the error message parameter (err) and returns the result when the write fails.
Case: read. js
[Code] var fs = require ('fs'); var buf = new buffer(102420.20.fs.open('input.txt ', 'r +', function (err, fd) {if (err) return console. error (err); console. log ('file opened successfully'); console. log ('Prepare to read file'); // fd fs. open ID // buf cache zone // 0, buf. length cache interval // 0, read input.txt start position fs. read (fd, buf, 0, buf. length, 0, function (err, bytes) {if (err) console. log (err); console. log (bytes + 'bytes read '); if (bytes> 0) {console. log (buf. slice (0, bytes ). toString ());}})})
Close file
[Code] fs. close (fd, callback) parameters are described as follows: fd-file descriptor returned through fs. open () method. Callback-callback function, with no parameters.
Case: close. js
[Code] var fs = require ('fs'); var buf = new buffer(102420.20.fs.open('input.txt ', 'r +', function (err, fd) {if (err) return console. error (err); console. log ('file opened successfully'); console. log ('Prepare to read file'); // fd fs. open ID // buf cache zone // 0, buf. length cache interval // 0, read input.txt start position fs. read (fd, buf, 0, buf. length, 0, function (err, bytes) {if (err) console. log (err); console. log (bytes + 'bytes read '); if (bytes> 0) {console. log (buf. slice (0, bytes ). toString () ;}}) // close the file fs. close (fd, function (err) {if (err) console. error (err); console. log ('file closed successfully ');});})
Intercepting a file
[Code] fs. ftruncate (fd, len, callback) This method uses a file descriptor to read the file descriptor returned by the file parameter fd-through the fs. open () method. Len-the length of the object content. Callback-callback function, no Parameter
Case: ftruncate. js
[Code] var fs = require ('fs'); var buf = new buffer(1024);lele.log('your open file 'your fs.open('input.txt ', 'r +', function (err, fd) {if (err) return console. error (err); console. log ('file opened successfully'); console. log ('file content after 10 byte truncation '); // captures the file fs. ftruncate (fd, 10, function (err) {if (err) console. log (err); console. log ('file intercepted successfully'); console. log ('read the same file'); fs. read (fd, buf, 0, buf. length, 0, function (err, bytes) {if (err) console. error (err); // only output the read bytes if (bytes> 0) {console. log (buf. slice (0, bytes ). toString ();} // close the file fs. close (fd, function (err) {if (err) console. error (err); console. log ('file closed successfully ');})})})
Delete an object
[Code] fs. unlink (path, callback) parameter path-file path callback-callback function, no Parameter
Case: unlink. js
[Code] var fs = ', function (err) {if (err) return console. log (err); console. log ('file deleted successfully ');})
Create directory
[Code] fs. mkdir (path [, mode], callback) parameter path-file path mode-Set directory permission. The default value is 0777 callback-callback function.
Case: mkdir. js
[Code] var fs = require ('fs'); console. log ('create directory test'); fs. mkdir ('test', function (err) {if (err) return console. error (err); console. log ('Directory created successfully ');});
View directory
[Code] fs. readdir (path, callback) parameters are described as follows: path-file path. Callback-callback function. The callback function has two parameters: err, files, err, and files.
Case: readdir. js
[Code] var fs = require ('fs'); console. log ('view/file directory'); fs. readdir ('.. /file/', function (err, files) {if (err) return console. log (err); files. forEach (function (file) {console. log (file );})})
Delete directory
[Code] fs. rmdir (path, callback) parameters are described as follows: path-file path. Callback-callback function, with no parameters.
Case: rmdir. js
[Code] var fs = require ('fs'); console. log ('delete/test directory'); fs. rmdir ('test', function (err) {if (err) console. error (err); console. log ('read/test directory'); fs. readdir ('test', function (err, files) {if (err) return console. log (err); files. forEach (function (file) {console. log (file );})})});
The above is the file operation for Node. js days. For more information, see PHP Chinese Network (www.php1.cn )!