This article is for node.js learning notes, not a complete list of fs apis. Reproduced in chapter 4.4 of the Node.js development guide for your own convenience. API document address, if there are shortcomings, welcome to point out, learn together.
function | An asynchronous function | Synchronization function |
---|---|---|
Open the file | fs.open(path,flags, [mode], [callback(err,fd)]) | fs.openSync(path, flags, [mode]) |
Close the file | fs.close(fd, [callback(err)]) | fs.closeSync(fd) |
Reading a file (file descriptor) | fs.read(fd,buffer,offset,length,position,[callback(err, bytesRead, buffer)]) | fs.readSync(fd, buffer, offset,length, position) |
Write file (file descriptor) | fs.write(fd,buffer,offset,length,position,[callback(err, bytesWritten, buffer)]) | fs.writeSync(fd, buffer, offset,length, position) |
Reading file contents | fs.readFile(filename,[encoding],[callback(err, data)]) | fs.readFileSync(filename,[encoding]) |
Write file contents | fs.writeFile(filename, data,[encoding],[callback(err)]) | fs.writeFileSync(filename, data,[encoding]) |
Delete the file | fs.unlink(path, [callback(err)]) | fs.unlinkSync(path) |
Create a directory | fs.mkdir(path, [mode], [callback(err)]) | fs.mkdirSync(path, [mode]) |
Delete the directory | fs.rmdir(path, [callback(err)]) | fs.rmdirSync(path) |
Read the directory | fs.readdir(path, [callback(err, files)]) | fs.readdirSync(path) |
Get the real path | fs.realpath(path, [callback(err,resolvedPath)]) | fs.realpathSync(path) |
rename | fs.rename(path1, path2, [callback(err)]) | fs.renameSync(path1, path2) |
truncation | fs.truncate(fd, len, [callback(err)]) | fs.truncateSync(fd, len) |
Change of ownership | fs.chown(path, uid, gid, [callback(err)]) | fs.chownSync(path, uid, gid) |
Change ownership (file descriptor) | fs.fchown(fd, uid, gid, [callback(err)]) | fs.fchownSync(fd, uid, gid) |
Change ownership (do not resolve symbolic links) | fs.lchown(path, uid, gid, [callback(err)]) | fs.lchownSync(path, uid, gid) |
Change the permissions | fs.chmod(path, mode, [callback(err)]) | fs.chmodSync(path, mode) |
Changing permissions (file descriptors) | fs.fchmod(fd, mode, [callback(err)]) | fs.fchmodSync(fd, mode) |
Changing permissions (without resolving symbolic links) | fs.lchmod(path, mode, [callback(err)]) | fs.lchmodSync(path, mode) |
Obtaining File Information | fs.stat(path, [callback(err, stats)]) | fs.statSync(path) |
Get file information (file descriptor) | fs.fstat(fd, [callback(err, stats)]) | fs.fstatSync(fd) |
Get file information (without parsing symbolic links) | fs.lstat(path, [callback(err, stats)]) | fs.lstatSync(path) |
Creating hard links | fs.link(srcpath, dstpath, [callback(err)]) | fs.linkSync(srcpath, dstpath) |
Creating symbolic links | fs.symlink(linkdata, path, [type],[callback(err)]) | fs.symlinkSync(linkdata, path,[type]) |
Read the link | fs.readlink(path, [callback(err,linkString)]) | fs.readlinkSync(path) |
Modify the file timestamp | fs.utimes(path, atime, mtime, [callback(err)]) | fs.utimesSync(path, atime, mtime) |
Modify the file timestamp | ||
(File descriptor) | fs.futimes(fd, atime, mtime, [callback(err)]) | fs.futimesSync(fd, atime, mtime) |
Synchronous disk cache | fs.fsync(fd, [callback(err)]) | fs.fsyncSync(fd) |