Nodejs Document Operation module FS (file System) Common functions Concise summary _node.js

Source: Internet
Author: User

There are many functions related to the operation of the system. Can be divided into two categories first.

The class is asynchronous + callback. The class is synchronized.

In this case only to the asynchronous collation, synchronization only need to be followed by the function name plus sync

1. The first is a class of most conventional reading and writing functions, function name and form, should be originated from the C language.

Copy Code code as follows:

Fs.open (file path, read-write identity, [file mode value, 666], callback function (err, file handle fd));
Fs.read (file handle FD, written buffer,offset,length,position, callback function (err, bytesread, buffer));
Fs.write (file handle FD, read Buffer,offset,length,position, callback function (Err,byteswritten,buffer));
Fs.close (file handle, callback function)
Fs.truncate (file handle, truncation length, callback function);
Fs.fsync (file handle, callback function);

2. Directly to read and write documents, it is more convenient to use.

Copy Code code as follows:

Fs.readfile (filename, encoding, callback function (Err,data));
Fs.writefile (filename, data, encoding, callback function (err));
Fs.appendfile (filename, data, encoding, callback function (err));


3. Other common file operation

Copy Code code as follows:

To determine whether a file exists
Fs.exists (file path, callback (if present));
Renaming
Fs.rename (old filename, new file name, callback function);
File owner Change
Fs.chown (filename, 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 (filename, 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 (filename, [Options],listener_callback (Stats of current file, stats before change));
Fs.unwatchfile (filename);


4. Directory operation

Copy Code code as follows:

Fs.mkdir (path, permission mode/777, callback function);
Fs.rmdir (path, callback function);
Fs.readdir (Path, callback function (Err,filenamearray));

5. Linked file operation

Copy Code code 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]);

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.