Nodejs A concise summary of common functions of the file Operation module FS (Files System)

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.

Fs.open (file path, read/write ID, [file mode value, 666], callback function (err, file handle fd));          Fs.read (file handle FD, written by 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. Read and write directly to the file, it is more convenient to use.

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

  

3. Other common file operations

Determine if the file exists      fs.exists (file path, callback (presence));      Rename      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 change      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);      Monitor file      fs.watchfile (file name, [Options],listener_callback (Stats of current file, stats before change));      

  

4. Directory Operations

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

  

5. Link file operations

Create a link   fs.link (srcpath, Dstpath, [callback])   fs.symlink (destination, path, [type], [callback])   Read the link to the path   fs.readlink (path, [Callback (ERR,LINKSTR)])   Fs.unlink (Path,[callback]);

  


Nodejs A concise summary of common functions of the file Operation module FS (Files System)

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.