According to Nodejs rookie tutorial finishing.
Official API Documentation:Nodejs file system API
Other Finishing: Nodejs File system operation function Classification
1. References:
Import file System module (FS) statement:var fs = require("FS")
2. Asynchronous and synchronous:
Async: Fs.readfile (); The first parameter is a callback error message.
Sync: Fs.readfilesync ();
eg
// Asynchronous Read function (err, data) { if (err) { return console.error (err); } Console.log ("asynchronous reads:" + data.tostring ());});
3. Open File:
Fs.open (path, flags[, mode], callback)
Where path is the file path, flags is the file open mode, mode is the file permission, callback (ERR,FD) is the callback function,
Where callback's second parameter, FD, is a file descriptor.
Source:
function (path, flags, mode, callback) { = Makecallback (arguments[arguments.length-1]); /* =0666 */ ); if return ; Binding.open (Pathmodule._makelong (path), stringtoflags (Flags), mode, callback);};
4. Get file information:
Fs.stat (Path, callback)
Where path is the file path, callback (err, stats) is the callback function, where the stats parameter is fs. The Stats object is also the file details.
5. Write to File:
Fs.writefile (filename, data[, Options], callback)
If the file exists, the content written by the method overwrites the old file content.
The parameters use the following instructions:
Path-file path.
Data-can be a string (string) or Buffer (stream) object to write to the file.
Options-This parameter is an object that contains {encoding, mode, flag}. Default encoding is UTF8, mode is 0666, flag is ' W '
Callback-callback function, the callback function contains only the error message parameter (ERR), which is returned when the write fails
6. Read the file:
Fs.read (fd, buffer, offset, length, position, callback)
The method uses the file descriptor (the callback parameter FD of the Open method) to read the file.
The parameters use the following instructions:
FD-The file descriptor returned by the Fs.open () method.
Buffer-Buffers for data writes.
Offset-The write offsets of the buffer write.
Length-the number of bytes to read from the file.
Position-the starting position of the file read, and if the value of position is NULL, it is read from the position of the current file pointer.
Callback-callback function, there are three parameters err, Bytesread, Buffer,err is the error message, Bytesread represents the number of bytes read, and buffer is the cache object.
7. Close the file:
Fs.close (FD, callback)
This callback finally has no parameter!!!
8. Interception of documents:
Fs.ftruncate (FD, Len, callback)
The parameters use the following instructions:
FD-The file descriptor returned by the Fs.open () method.
Len-Length of file content interception.
Callback-callback function, no parameters.
9. Delete files:
Fs.unlink (Path, callback)
The callback of the deleted file also has no parameters.
10. Create a directory:
Fs.mkdir (path[, mode], callback)
The parameters use the following instructions:
Path-file path.
Mode-Set directory permissions, default to 0777.
Callback-callback function, no parameters.
11. Read the directory:
Fs.readdir (Path, callback)
Callback has two parameters, where err is the error message and files is the list of file arrays in the current directory.
12. Delete Directory
Fs.rmdir (Path, callback)
This callback is not a parameter.
File System (node. JS Learning Note)