node. js File System

Source: Internet
Author: User
Tags readfile

node. js File System

node. JS provides a set of UNIX-like (POSIX)-compliant file manipulation APIs. The Node import file System module (FS) syntax is as follows:

var=require("FS")    
Asynchronous and synchronous

The methods in the node. js file System (FS module) module have asynchronous and synchronous versions, such as functions that read the contents of the file with asynchronous Fs.readfile () and synchronous Fs.readfilesync ().

The last parameter of the Async method function is a callback function, and the first parameter of the callback function contains the error message (ERR).

We recommend that you use asynchronous methods, which are more efficient, faster, and not blocking than synchronous.

Instance

Create a Input.txt file with the following content:

Novice Tutorial Official website address: www.runoob.com file Read instance

  

Create the File.js file with the following code:

var fs = require ("FS");//asynchronously reads Fs.readfile (' Input.txt ', function (err, data) {   if (err) {       return Console.error (err );   }   Console.log ("asynchronous reads:" + data.tostring ());}); /synchronous Read var data = Fs.readfilesync (' input.txt '); Console.log ("synchronous read:" + data.tostring ()); Console.log ("program execution is complete. ");

  

The result of the above code execution is as follows:

$ node File.js synchronous read: Novice Tutorial official website address: www.runoob.com file read instance program execution completed. Asynchronous read: Novice Tutorial official website address: www.runoob.com file Read instance

  

Excerpt from: http://www.runoob.com/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.