Nodejs FS Module

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.