Nodejs File operation module FS (File System) common functions summary
- 2020-03-30 03:11:02
- OfStack
There are many functions related to system operation. First, there are two broad categories.
One is asynchronous + callback. One is synchronous.
In this case, only the asynchronous ones are cleaned up, and the synchronous ones just need to add Sync after the function name
1. The first is a class of the most common read-write functions, the function name and form, should be derived from the C language.
fs.open( The file path , Read/write identification ,[ file mode value ,666], The callback function (err, File handle fd));
fs.read( File handle fd, Be written to the buffer,offset,length,position, The callback function (err, bytesRead, buffer));
fs.write( File handle fd, Be read buffer,offset,length,position, The callback function (err,bytesWritten,buffer));
fs.close( File handle , The callback function )
fs.truncate( File handle , Truncation length , The callback function );
fs.fsync( File handle , The callback function );
2. Directly read and write files, it is more convenient to use.
fs.readFile( The file name , coding , The callback function (err,data));
fs.writeFile( The file name , data , coding , The callback function (err));
fs.appendFile( The file name , data , coding , The callback function (err));
3. Other common file operations
Determine if the file exists
fs.exists( The file path ,callback( If there is a ));
rename
fs.rename( The old name of the file , The new file name , The callback function );
File owner change
fs.chown( The file name ,uid,gid, The callback function );/fs.fchown( File handle fd,uid,gid, The callback function );/fs.lchown( Link path ,uid,gid, The callback function );
File permission change
fs.chmod( The file name ,mode, The callback function );/fs.fchmod( File handle ,mode, The callback function );/fs.lchmod( Link path ,mode, The callback function );
File information
fs.stat( The file path , The callback function (err.fs.Stats object ));/fs.fstat( File handle fd, The callback function (err.fs.Stats object ));/fs.lstat( Link path , The callback function (err.fs.Stats object ));
File time
fs.utimes( The file path , Access time , The new time , The callback function );/fs.futimes( File handle , Access time , The new time , The callback function );
Monitor file
fs.watchFile( The file name ,[options],listener_callback( Current file's stats, Before the change stats));
fs.unwatchFile( The file name );
4. Directory operation
fs.mkdir( The path , permissions mode/777, The callback function );
fs.rmdir( The path , The callback function );
fs.readdir( The path , The callback function (err,fileNameArray));
5. Link file operation
Create a link
fs.link(srcpath, dstpath, [callback])
fs.symlink(destination, path, [type], [callback])
Read the path to which the link points
fs.readlink(path, [callback(err,linkstr)])
fs.unlink(path,[callback]);