Nodejs files, directory operations (1)

Source: Internet
Author: User

First, we divide the FS file system into two categories: the first is the XXX method and the second is the Xxxsync method. Almost all FS operations are in these two categories, the first is asynchronous callbacks, and the second is synchronous waiting.

  A. Read and write operations for files

A. Integrity reading and writing

1.fs.readfile (Filename,[options],callback)//can be used to fully read an entire file.

2.fs.writefile (Filename,data,[options],callback)//write data to file

3.fs.appendfile (Filename,data,[options],callback)//write file data

B. Specify the location to read and write

First open the file, then manipulate the file, and finally close the file.

A. Fs.open (Filename,flags,[mode],callback) callback (ERR,FD)//Open File

B. Fs.read (Fd,buffer,offset,length,position,callback)//Read file

C. Fs.write (Fd,buffer,offset,length,position,callback)//write file

D. Fs.fsync (Fd,callback)//This function is called when the above Read,write asynchronous operation is complete, and the close operation is finally performed.

E. fs.close (FD)//close file

  B. Creating and reading catalogs

1. Fs.mkdir (path,callback);//Create Directory

2. Fs.readdir (path,callback);//Read directory, where files in callback is the current directory

3. Fs.stat (path,callback);//view directory and file information

4. Fs.lstat (path,callback);//view directories and files as well as soft-chain information, as long as you want to see the soft chain, you have to look at this.

5. Fs.exists (path,callback);//check for the existence of this directory, or file.

  C. Other operations on the directory

      1. Fs.realpath (path,[cache],cakkback);//Get the absolute path of the current directory or file

2. Fs.utimes (path,atime,utime,callback);//modify file access and modification times

3. Fs.chmod (path,mode,callback);//Modify the file permissions

4. Fs.rename (oldpath,newpath,callback);//modify and move files from Oldpath->newpath

5. Fs.link (srcpath,dstpath,callback);//Create a hard link (only in this voucher) Fs.unlink () Remove hard links

6. Fs.symlink (srcpath,stpaht,callback);//Create a soft link (any coupon) Fs.readlink (path,callback) Read all the information of the soft link.

7. Fs.truncate (filename,len,callback);//truncate the file, leave the file part left behind

8. Fs.rmdir (path,callback);//delete directory

Example: Https://github.com/caixiaojia/node-example/tree/master/fs_example

Nodejs files, directory operations (1)

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.