Node File Operations

Source: Internet
Author: User


As a front-end small wire, it is operable for other languages. Is it true that our small front-end is a little envious and jealous, and alert and console in the browser debugging window silently? T.T.
Therefore, the File operations in node give us light. This time, we will simply talk about the File System sub-tasks in node. This article mainly includes the APIs for File operations, and awesome Stream operations ~, Okay, so stay with cainiao. take the lead ~

File System: For File operations, the module fs is the main one here. The fs module in node provides many APIs, but all its methods are synchronous and asynchronous. For reading file content, the most important thing to note is the issue of controlling the execution process between asynchronous and synchronous ~

 
 
  1. Var fs = require ('fs ');
  2. // The asynchronous callback method is asynchronous, so we need to use the callback method to process the data read operations.
  3. // This should be used to frontend development accustomed to event operations.
  4. Fs. readFile ('data. json', function (err, data ){
  5. If (err) {} else {
  6. Console. log (data. length );
  7. }
  8. });
Result:


Every asynchronous API has its callback function that can be used, so the following method will report an error, just like the setTimeout used in JS,

 
 
  1. Var fs = require ('fs ');
  2. // There will be errors because the file is read asynchronously and the data has not been read from the console.
  3. Var data = fs. readFile ('data. json ');
  4. Console. log (data. length );
Result:


Or simply use the corresponding synchronization API.

 
 
  1. Var fs = require ('fs ');
  2. // Or use the synchronized API to read data
  3. Var data = fs. readFileSync ('data. json ');
  4. Console. log (data. length );
Result:


Other simple APIs

 
 
  1. Fs.writeFile('delete.txt ', '2013', function (err ){
  2. Console ('youxi! ');
  3. });

  4. // Delete an object
  5. Fs.unlink('delete.txt ', function (){
  6. Console. log ('success ');
  7. });

  8. // Modify the file name
  9. Fs.rename('delete.txt', 'anew.txt ', function (err ){
  10. Console. log ('rename success ');

  11. // View the File status
  12. Fs.stat('ane0000txt ', function (err, stat ){
  13. Console. log (stat );
  14. });
  15. });

  16. // Determine whether a file exists
  17. Fs.exists('a.txt ', function (exists ){
  18. Console. log (exists );
  19. });


File System API
Fs. open (path, flags, [mode], callback );
Flags: 'r'-open the file in read-only mode. If the file does not exist, an exception occurs. 'r + '-open the file in read/write mode, an exception occurs when the file does not exist. 'rs '-the file is opened synchronously in read-only mode, and the local cached file is bypassed for system operation 'rs +'-the file is opened synchronously in read/write mode, bypass the local cache file for system operation 'W'-open the file in write-only mode. When the file does not exist, create the file, or overwrite the 'wx 'file when it exists-it is consistent with 'W', but it is only applicable when the file does not exist (during the test, the node version is v0.10.26, if the file does not exist, the file is created normally and data is written. However, if the file does not exist, the following error occurs: you must write callback. After callback is added, no error is reported but no operation is performed. ) 'W + '-open the file in read/write mode 'ws +'-consistent with 'W +, however, this only applies when the file does not exist. 'A'-open the file by adding data. If the file does not exist, create 'a + '-to open the file by adding and reading, if the file does not exist, create 'ax + ', which is the same as 'a +', but fails to exist.
Mode: sets the file mode. The default value is 0666, Which is readable and writable.
Callback: two parameters (err, fd) are provided)

Fs. readFile (filename, [optins], callback );
Filename: String option: Object encoding: String | Null, default = Null flag: String default = 'R' callback: Function // callback has two parameters (err, data ), similar to most callback interfaces in node.

Fs. writeFile (filename, data, [optins], callback );
Filename: String data: String | Buffer (which will be introduced later) option: Object encoding: String | Null, default = 'utf8' mode: Number default = 438 flag: string default = 'W' callback: Function

// Add data to the end of the file fs. appendFile (filename, data, [optins], callback );
Filename: String data: String | Buffer option: Object encoding: String | Null, default = 'utf8' mode: Number default = 438 flag: String default = 'W' callback: Function
The above commonly used asynchronous APIs have their corresponding synchronous APIs. Adding Sync after the asynchronous API is the synchronous API. For more API please refer to official documentation http://nodejs.org/api/fs.html


Stream
Stream is one of the highlights of node, which is closely related to big data processing.

 
 
  1. var fs = require('fs');
  2. function copy( src, dest ){
  3. fs.writeFileSync( dest, fs.readFileSync(src) );
  4. }
  5. copy('data.json', 'dataStream.json');

The above is a copy of the file code, it seems that there is no problem, but it is indeed no big problem when processing small files, but once the processing of a large number of files can be seen, first, read the data, write the data, and use the memory as a transit. If the file is too large, the problem may occur.
When you need to process large files, you need to use several other APIs of file system, createReadStream and fs. createWriteStream processes a file as a small data stream, rather than a whole large data block.

 
 
  1. // It may also occur that the data written in the memory burst cannot keep up with the reading speed, and files that have been read continuously are stored in the memory.
  2. // However, the two operations are absolutely different, so the unwritten data keeps increasing in the memory, which may lead to memory burst.
  3. Var fs = require ('fs ');
  4. Var rs = fs. createReadStream ('data. json ');
  5. Var ws = fs. createWriteStream ('datastream. json ')
  6. Rs. on ('data', function (chunk ){
  7. Console. log ('data chunk read OK ');
  8. Times ++;
  9. Ws. write (chunk, function (){
  10. Console. log ('data chunk write OK ');
  11. });
  12. });
  13. Rs. on ('end', function (){
  14. Console. log (times );
  15. });
Result:

It can be seen that the reading speed and writing speed are different. The first data block is written only after two data blocks are read at the beginning, memory problems may occur when a large amount of data is accumulated. Therefore, you need to improve the stream operation.

 
 
  1. Var fs = require ('fs ');
  2. Var rs = fs. createReadStream ('data. json ');
  3. Var ws = fs. createWriteStream ('datastream. json ')
  4. // For example 1, you can view the data stream drain event to indicate that writing only data streams has written the data in the cache to the target.
  5. Rs. on ('data', function (chunk ){
  6. Console. log ('data chunk read OK ');
  7. If (ws. write (chunk, function (){
  8. Console. log ('data chunk write OK ')
  9. }) = False ){
  10. Rs. pause ()
  11. }
  12. });
  13. Rs. on ('end', function (){
  14. Ws. end ()
  15. });
  16. Ws. on ('drain', function (){
  17. Rs. resume ();
  18. });
Result:


Or:
 
 
  1. //eg2
  2. function reStartRead( rs ){
  3. console.log('lasted readed data write OK, reStart Read.');
  4. rs.resume();
  5. }
  6. rs.on('data',function(chunk){
  7. console.log('data chunk read ok' );
  8. if( ws.write(chunk,function(){
  9. reStartRead( rs );
  10. }) == false ){
  11. rs.pause()
  12. }
  13. });
  14. rs.on('end',function(){
  15. ws.end()
  16. });
Result:


The preceding two methods are equivalent to pausing reading a data block each time until the data block has been written.
In this case, the node has a pipe method connecting two data streams, reading and writing data like a catheter.
 
 
  1. function copy( src, dest ){
  2. fs.createReadStream( src ).pipe( fs.createWriteStream( dest ) );
  3. }
  4. copy('data.json', 'dataStream.json');

Some of the methods and events used in the stream above, more API content please refer to the official website http://nodejs.org/api/stream.html



Related Article

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.