The node. JS Module FS File system

Source: Internet
Author: User
Tags posix readfile

FS module is the encapsulation of file operation, it provides the file read, write, rename, delete, traverse directory, link and other POSIX file system operation. Unlike other modules, all operations in the FS module provide asynchronous and synchronized two versions, such as functions that read the contents of a file with asynchronous Fs.readfile () and synchronous Fs.readfilesync (). We represent the functions that are commonly used by FS and list the definitions and functions of all FS functions.

Fs.readfile (Filename,[encoding],[callback (Err,data)) is the simplest function to read a file. It takes a required parameter, filename, to indicate the name of the file to read. The second parameter, encoding, is optional, which represents the character encoding of the file. Callback is a callback function that is used to receive the contents of a file. If you do not specify encoding, then callback is the second parameter. The callback function provides two parameters, err and data,err, indicating that no error occurred and that data is the file content. 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:

var fs = require (' FS ');
Fs.readfile (' Content.txt ', function (err, data) {
if (err) {
Console.error (ERR);
} else {
Console.log (data);
}
});
Suppose the content in Content.txt is an example of a UTF-8 encoded text file, and the result is as follows:
<buffer, E6, E6 9c ac e6, E4, bb b6 e7 A4 ba e4 be 8b>
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:

var fs = require (' FS ');
Fs.readfile (' Content.txt ', ' utf-8 ', function (err, data) {
if (err) {
Console.error (ERR);
} else {
Console.log (data);
}
});
The result of the operation is:
Example text text file

The asynchronous programming interface of node. JS is used to take the last parameter of a function as a callback function,
Often a function has only one callback function. The callback function is the first of the actual arguments is err, whose
The remainder parameter is the other returned content. If no error occurs, the value of ERR will be null or
Undefined If an error occurs, err is usually an instance of the Error object.

Fs.readfilesync (filename, [encoding]) is a version of Fs.readfile synchronization. It accepts
The parameters are the same as fs.readfile, and the contents of the read file are returned as function return values. If there is an error
If the error occurs, FS throws an exception, and you need to catch and handle the exception using try and catch.

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

Fs.open (path, flags, [mode], [Callback (Err, FD)]) are the POSIX open functions
Encapsulation, similar to the fopen function in the C language standard library. It accepts two required parameters, path is the file
Flags can be the following values.
? R: Open the file in read mode.
? r+: Open file in read-write mode.
? W: Open the file in write mode and create if the file does not exist.
? w+: Open the file in read-write mode and create if the file does not exist.
? A: Open the file in Append mode and create if the file does not exist.
? A +: Opens the file in read Append mode and creates if the file does not exist.
The mode parameter assigns permissions to the file when it is created, and defaults to 0666①. The callback function will pass a text
Descriptor Fd②.

Fs.read (fd, buffer, offset, length, position, [Callback (Err, Bytesread,buffer)]) is the encapsulation of the POSIX read function, which provides a lower level of Interface. The function of Fs.read is to read the data from the specified text descriptor fd and write to the buffer object pointed to in buffer. offset is the write offset of the buffer. Length is the number of bytes to read from the file. Position is the starting position of the file read, and if the value of position is NULL, it is read from the position of the current file pointer. The callback function passes Bytesread and buffers, representing the number of bytes read and the buffer object, respectively. The following is an example of using Fs.open and Fs.read.

varFs=require ("FS"); Fs.open ("Data.txt", "R",function(ERR,FD) {if(Err) {Console.log (err); return; }    varbuf=NewBuffer (8); Fs.read (Fd,buf,0,8,NULL,function(err,bytesread,buffer) {if(Err) {Console.log (err); return; } console.log ("Bytesread:" +bytesread);    Console.log (buffer); })})

In general, do not use this method to read a file unless necessary, because it requires you to manually manage the buffer
and file pointers, especially if you don't know the size of the file, it's going to be a hassle.

The node. JS Module FS 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.