There are quite a lot of functions related to the operation of the component system. First, it can be divided into two categories.
One type is asynchronous + callback. One category is synchronous.
Here, only asynchronous sorting is done. Only synchronous needs to add Sync after the function name.
1. First of all, the most common type of read and write functions, the function name and form should originate from C language.
The code copy is as follows:
fs.open(file path, read and write identifier, [file mode value, 666], callback function (err, file handle fd));
fs.read(file handle fd, buffer, offset, length, position, callback function (err, bytesRead, buffer) written);
fs.write(file handle fd, buffer, offset, length, position, callback function (err, bytesWritten,buffer) being read);
fs.close (file handle, callback function)
fs.truncate (file handle, truncated length, callback function);
fs.fsync (file handle, callback function);
2. It is easier to use by reading and writing files directly.
The code copy is as follows:
fs.readFile(file name, encoding, callback function (err, data));
fs.writeFile(file name, data, encoding, callback function (err));
fs.appendFile(file name, data, encoding, callback function (err));
3. Other common file operations
The code copy is as follows:
Determine whether the file exists
fs.exists(file path, callback(whether it exists));
Rename
fs.rename (old file name, new file name, callback function);
File owner changes
fs.chown(file name, uid, gid, callback function);/fs.fchown(file handle fd, uid, gid, callback function);/fs.lchown(link path, uid, gid, callback function);
File permission changes
fs.chmod(file name, mode, callback function);/fs.fchmod(file handle, mode, callback function);/fs.lchmod(link path, mode, callback function);
File information
fs.stat (file path, callback function (err.fs.Stats object));/fs.fstat (file handle fd, callback function (err.fs.Stats object));/fs.lstat (link path, callback function (err.fs.Stats object));
File time
fs.utimes(file path, access time, new time, callback function);/fs.futimes(file handle, access time, new time, callback function);
Monitoring files
fs.watchFile(file name, [options], listener_callback(current file stats, before changing stats));
fs.unwatchFile(file name);
4. Directory operation
The code copy is as follows:
fs.mkdir(path, permission mode/777, callback function);
fs.rmdir (path, callback function);
fs.readdir(path, callback function (err, fileNameArray));
5. Link file operation
The code copy is as follows:
Create a link
fs.link(srcpath, dstpath, [callback])
fs.symlink(destination, path, [type], [callback])
Read the path to which the link is pointed
fs.readlink(path, [callback(err,linkstr)])
fs.unlink(path,[callback]);