Hello everyone) Please help to understand the work with the file system on the node. I searched for info in the official documentation http://nodejs.org/api/fs.html , as well as on some other sites. And there were a few questions.

The official dock has such a method for writing text to a file. http://nodejs.org/api/fs.html#fs_fs_writefile_filename_data_encoding_callback :

 fs.writeFile(filename, data, [encoding], [callback]); 

This method works fine, but as I roam the network, I came across another code (this method is also off-dock, but it is poorly commented on there):

 fs.open("file.txt", "a", 0644, function(err, file_handle) { if (!err) { fs.write(file_handle, 'текст', null, 'ascii', function(err, written) { if (!err) { // Всё прошло хорошо, делаем нужные действия и закрываем соединение с файлом. fs.close(file_handle); } else { // Произошла ошибка при записи } }); } else { // Обработка ошибок при открытии } }); 

So what's the difference? In my opinion it is better to use the first option, since there are fewer operations, and less code. But is it?

  1. How to move files / folders?
  2. How to determine file size?

I ask you not to throw at me, I have never worked with files before, maybe I’m asking stupid questions about it.

    2 answers 2

    fs.open used to open a file descriptor (in the docks, all functions in the arguments of which is a variable fd - they expect it, since T. JavaScript'a - the file descriptor is just an integer) and further work with it, fs.writeFile - strictly for writing to a file. You can open and close the descriptor, that is, by getting a file_handle (aka fd ), you can easily and simply write and read.

    To make it clearer, imagine that writeFile is just a utility, the capabilities of which end in writing to a file, i.e. - we do not have any access to its statistics, we can not view it, delete it, etc., all we can do is write to a file, besides, after recording, the utility dies (i.e. we have the ability to single record). In the second case, roughly speaking - we opened a certain editor and we can do everything - read, look at the file, write, etc. (if this is rude :)). Just do not forget "it" to close.

    About "it is better to use the first option" - depends on the task. If you just need to "write and forget" - then yes, the first is better; if not, the second is better (although again it depends on the task, since the written code - the first option is still shorter and more beautiful, on the other hand - the code using fs.open more flexible)

    Other questions:


     fs.rename( '/path/to/the/file/filename.ext', '/new/path/to/the/file/filename.ext', function( err ) { /* */ } ) 


     fs.stat( 'path/to/the/file/filename.ext', function( err, stat ) { if( !err ) { console.log( stat ); // -> file stats hash console.log( stat.size ); // -> file size } }) 
    • Thanks for the informative answers! Honestly, I thought that fs.rename () can only rename) - LightShock
    • By the way, besides the size property, there are quite a few other properties in the resulting file hash. Could you explain what they mean, otherwise I did not find it in the documentation. - LightShock

    How to determine file size?

    Class: fs.Stats

    How to move files / folders?

    First you need to copy the file, and then delete the initial file:

     var fs = require('fs'), util = require('util'); var is = fs.createReadStream('source_file') var os = fs.createWriteStream('destination_file'); util.pump(is, os, function() { fs.unlinkSync('source_file'); }); 
    • 2
      The method of moving files you described smiled ... Besides, why stupidly copy-paste the code from the first pages of Google issue? :) - Zowie