node. js File System

Source: Internet
Author: User

Three related methods were tested

Read file method, open file method, write data method

Fs.readfile

Fs.open

Fs.write

node. js File System

The node. js file system is encapsulated in the FS module, which provides file read, write, rename, delete, traverse directories, links and other POSIX file system operations.

Unlike other modules, all operations in the FS module provide asynchronous and synchronous two versions, such as functions that read the contents of a file with asynchronous Fs.readfile ()

and the fs.readfilesync of the same step ().

Fs.readfile
Fs.readfile (Filename,[encoding],[callback (Err,data))

    • FileName (required), which indicates the name of the file to read.
    • Encoding (optional), which represents the character encoding of the file.
    • Callback is a callback function that is used to receive the contents of a file. (data is the read-from of the return.) if Encoding,data is specified as a parsed string, data will be binary data in the form of Buffer. )
For example, we read the data from the Content.txt, but do not specify the encoding:


Suppose the content in Content.txt is an example of a UTF-8 encoded text file, and the result is as follows:



This program reads the contents of the file in binary mode, and the value of data is the Buffer object.


If we give fs.readfile encoding Specifies the encoding:

The result of the operation is:

Example text text file

Fs.open
Fs.open (path, [mode], [Callback (Err, FD)]
<span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, Sans-serif; line-height:16.7999992370605px; " >path is the path to the file, mode is read/write (<span style= "color: #ff0000;" > Common A-mode </span>) </span>
<span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, Sans-serif; line-height:16.7999992370605px; " >FD is a file descriptor (used to open the read-write file method in the file method) </span>
<span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, Sans-serif; line-height:16.7999992370605px; " ></span><ul style= "LINE-HEIGHT:16.7999992370605PX; font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, Sans-serif; " ><li style= "padding:2px 0px;" >r: Opens the file in read mode, where the data flow is located at the beginning of the file. </li><li style= "padding:2px 0px;" >r+: Open file in read-write mode, <span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, Sans-serif; font-size:14px; line-height:16.7999992370605px; White-space:pre; BACKGROUND-COLOR:RGB (240, 240, 240); " > Data Flow location at the beginning of the file </span>. </li><li style= "padding:2px 0px;" >w: Open the file in write mode, if the file does not exist then create, the file exists is zeroed, <span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial , sans-serif;font-size:14px; line-height:16.7999992370605px; White-space:pre; BACKGROUND-COLOR:RGB (240, 240, 240); " > Data Flow location at the beginning of the file </span>. </li><li style= "padding:2px 0px;" >w+: Open file in read-write mode, if the textThe piece does not exist then create <span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, sans-serif;font-size:14px; line-height:16.7999992370605px; White-space:pre; BACKGROUND-COLOR:RGB (240, 240, 240); " The file exists then zeroed, </span><span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, sans-serif;font-size:14px; line-height:16.7999992370605px; White-space:pre; BACKGROUND-COLOR:RGB (240, 240, 240); " > Data Flow location at the beginning of the file </span>. </li><li style= "padding:2px 0px;" >a: Open file in Append mode, create if file does not exist, <span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, sans-serif;font-size:14px; line-height:16.7999992370605px; White-space:pre; BACKGROUND-COLOR:RGB (240, 240, 240); " > Data Flow location append </span> at end of file. </li><li style= "padding:2px 0px;" >a+: Open file in read append mode, create if file does not exist, <span style= "font-family: ' Microsoft yahei ', ' Helvetica Neue ', Helvetica, Arial, sans-serif;font-size:14px; line-height:16.7999992370605px; White-space:pre;BACKGROUND-COLOR:RGB (240, 240, 240); " The > Data Flow location is appended at the end of the file. </span></li></ul><div></div><div>


  • FD: Corresponding file descriptor
  • Buffer: The data to write to the buffer
  • Offset: The starting position of the data to be written
  • Length: data lengths to write
  • Position: Writes data from where in the file, and if the value of position is NULL, it is read from the position of the current file pointer (because the Append mode is open and the pointer is at the end of the file).
  • Callback: The callback function passes the Byteswrite ( number of bytes written ).
Fs.open (' Writefile.txt ', ' a ', function opend (err, FD) {if (err) {console.error (err); return;} var writebuffer = new Buff ER (' write file 123 writes//>>>> '), bufferposition = 0,bufferlength = Writebuffer.length,fileposition = Null;fs. Write (FD,     WriteBuffer,     bufferposition,     bufferlength,     fileposition,     function wrote (err, Written) {           if (err) {throw err;}   Console.log (ERR); });  


Reference http://www.w3cschool.cc/nodejs/nodejs-fs.html








node. js File 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.