node. js File System

Source: Internet
Author: User

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 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

node. js reads the file function syntax as follows:

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.

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:

Varfs = require (' FS '); Fs.readfile (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:

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 (function(err, data) {     if  (err) {         console.error (err);      Else {         console.log (data);     

The result of the operation is:

Example text text file

Err will be the error object when reading a file with errors. If Content.txt does not exist, running the preceding code will result in the following:

{[Error:enoent, no such file or directory ' Content.txt '] errno:34, code: ' ENOENT '
Fs.readfilesync

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

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

Fs.open

Fs.open (path, flags, [mode], [Callback (Err, FD)]) is the encapsulation of the POSIX open function, similar to the fopen function in the C language standard library. It accepts two required parameters, path is the file's paths, and 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
Fs.read

The Fs.read syntax format is as follows:

Fs.read (fd, buffer, offset, length, position, [Callback (Err, bytesread, buffer)])

Parameter description:

    • FD: Reads the data and writes 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: 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.
    • Callback: The callback function passes the bytesread and buffers, respectively, representing the number of bytes read and the buffer object.

The following is an example of using Fs.open and Fs.read.

Varfs = require (' FS '); Fs.open (' Content.txt ', ' R ',function(Err, FD) {if(Err) {console.error (err); return; } varbuf= Newbuffer (8); Fs.read (FD, buf,0, 8,NULL,function(err, bytesread, buffer) {if(Err) {console.error (err); return; } console.log (' Bytesread: ' +bytesread);     Console.log (buffer); }) }); 

The operating result is:

In general, do not use this method to read a file unless necessary, because it requires you to manually manage buffers and file pointers, especially if you do not know the size of the file, which can be a cumbersome task.

FS Module function table

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.