There are quite a few functions related to the system operation. Can be divided into two major categories first.
The class is asynchronous + callback. The class is synchronous.
In this case only the asynchronous collation, synchronous only need to add the function name after the sync
1. The first is a class of the most conventional read-write functions, function names and forms, should be originated in the C language.
The code is as follows:
Fs.open (
filePath
Read/writeidentification, [
fileMode value, 666], callback function (err,
filehandle FD));
Fs.read (
fileHandle FD, written by Buffer,offset,length,position, callback function (err, bytesread, buffer));
Fs.write (
fileHandle FD, read Buffer,offset,length,position, callback function (Err,byteswritten,buffer));
Fs.close (
fileHandle, callback function)
Fs.truncate (
fileHandle, truncation length, callback function);
Fs.fsync (
fileHandle, callback function);
2. read and write directly to the file , it is more convenient to use.
The code is as follows:
Fs.readfile (
fileName, code, callback function (Err,data));
Fs.writefile (
fileName, data, encoding, callback function (err));
Fs.appendfile (
fileName, data, encoding, callback function (err));
3. Other common file operations
The code is as follows:
Judge
fileWhether there is
Fs.exists (
filePath, callback (presence));
Renaming
Fs.rename (old
fileName, new
fileName, callback function);
fileOwner Change
Fs.chown (
fileName, Uid,gid, callback function);/fs.fchown (
fileHandle Fd,uid,gid, callback function);/fs.lchown (link path, Uid,gid, callback function);
file
PermissionsChange
Fs.chmod (
fileName, mode, callback function);/fs.fchmod (
fileHandle, mode, callback function);/fs.lchmod (link path, mode, callback function);
fileInformation
Fs.stat (
filePath, callback function (Err.fs.Stats object));/fs.fstat (
fileHandle FD, Callback function (Err.fs.Stats object));/fs.lstat (link path, callback function (Err.fs.Stats object));
fileTime
Fs.utimes (
filePath, access time, new time, callback function);/fs.futimes (
fileHandle, access time, new time, callback function);
Monitoring
file
Fs.watchfile (
fileName, [Options],listener_callback (current
fileStats, stats before change);
Fs.unwatchfile (
filename);
4. Directory Operations
The code is as follows:
Fs.mkdir (Path,
PermissionsMode/777, callback function);
Fs.rmdir (path, callback function);
Fs.readdir (Path, callback function (Err,filenamearray));
5. Link file operations
The code is as follows:
Create a link
Fs.link (Srcpath, Dstpath, [callback])
Fs.symlink (destination, path, [type], [callback])
Read the path that the link points to
Fs.readlink (Path, [Callback (ERR,LINKSTR)])
Fs.unlink (Path,[callback]);
Nodejs FS Module