Lnode.js Study Notes 7

Source: Internet
Author: User

File System FS

The FS module is the encapsulation of file operations, which provides file read, write, rename, delete, traverse directory, link and other POSIX (note: Portable Operating System Interface: Portable Operating system Interface, abbreviated POSIX) file system operation.

All operations in the FS module provide two versions of asynchronous (Asyn) and synchronous (Syn) , such as asynchronous Fs.readfile () and synchronous Fs.readfilesync () that read the contents of the file.

1. Fs.readfile (filename, [encoding], [Callback (Err,data)]) is the simplest function to read a file.

FileName: The name of the file to read.

Encoding: The character encoding of the file.

Callback: callback function to receive the contents of a file. Receive two parameters err, and data, err indicates that there is no error occurred, and that data is the file content. If Encoding,data is specified as a parsed string, the

Otherwise, data will be binary data in the form of buffer .

err is null if file can be read normally

Tip: The asynchronous programming interface of node. JS is accustomed to using the last parameter of a function as a callback function, usually a function with only one callback function. The callback function is the first err in the actual argument, and the rest of the arguments are the other returned content.

If no error occurs, the ERR value will be null or undefined. If an error occurs, err is usually an instance of the Error object.

Tip: Unlike synchronous I/O functions, most async functions in node. JS have no return value.

2.fs.open (path, flags, [mode], [Callback (ERR,FD)]) is the encapsulation of the open function in POSIX.

Path: Paths to files (recommended relative paths for portability)

Flags

R: Open the file as read-only.

r+: Open the file as read-write.

W: Open the file in write mode and create if the file does not exist.

w+: Opens the file in read-write mode and creates if the file does not exist.

A: Open the file in Append mode and create if it does not exist.

A +: Opens the file in read Append mode and creates if the file does not exist.

Mode: Specify permissions when creating files, default is 0666. The callback function will pass a file descriptor FD.

Note:

(1) Specify Permissions 0666: File permissions refer to the specification of file read and access permissions in the POSIX operating system, usually in an octal representation.

For example, 0754 indicates that the permissions of the file owner are 7 (read, Write, execute: 0111), the permissions of the user in the same group are 5 (read, execute 0101), and the other user's permissions are 4 (read 0100).

Then: 0666: The file owner, the same group of users and other users ' permissions are 6 (read, write 0110).

(2) The file descriptor is a non-negative integer that represents the index of the record table of open files maintained by the operating system kernel for the current process.

3.fs.read (Fd,buffer,offset,length, Position,[callback (Err,bytesread,buffer)]) is the encapsulation of the Posixread function, Compared to Fs.readfile provides a lower interface,

The function of Fs.read is to read the data from the specified file descriptor fd and write to the buffer object that buffer points to.

Offset is the write offset of the buffer.

Length: Is the number of bytes to read from the file.

Position: The starting position of the file read, and if the value of position is NULL, it is read from the location of the file pointer.

Bytesread indicates the number of bytes read.

Buffer: Represents the Buffer object.

In general, unless necessary, generally do not use the Fs.read method to read the file, because he will ask you to manually manage the buffer and file pointers, especially when you do not know the size of the file, this will be a very troublesome thing.

4.fs.writefile (Filename,data,[encoding],[callback (err)])

If the filename file does not exist, the file is created and the content is written to data. If the filename file already exists, data overwrites the original content, which is equivalent to rewriting it!

Lnode.js Study Notes 7

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.