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?
- How to move files / folders?
- 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.