How to copy files in Nodejs and copy files in Nodejs
As the front-end kids shoes know, javascript does not have the permission to operate disk files, and server kids shoes have always been despised. However, node. js has made our front-end very proud. Recently, we have been learning node. Its powerful functions are so exciting and exciting. Today I learned how to read and write files.
First, introduce the fs module, which comes with nodejs.
Var fs = require ("fs ");
For details, refer to Nodejs API:Http://www.w3cfuns.com/tools.php? Mod = booknodejs
Two methods are used:
1. fs. readFile (filename, [encoding], [callback])
This is asynchronous file reading, filename is the file path, encoding is the encoding format, and callback is the callback function.
An example of Asynchronously reading all the content of a file is as follows:
fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; console.log(data);});
Here I am using a local test file:
Function readFile () {console. log ('-------- start reading file --------'); var fs = require ('fs'); fs.readFile('test.txt ', 'utf-8', function (err, data) {if (err) {console. log ("failed to read");} else {console. log (data); return data ;}}); console. log ('-------- read ended --------');}
2. fs. writeFile (filename, data, encoding = 'utf8', [callback])
Write File:
Function writeFile (data) {fs. writeFile ("test2.txt", data, function (error) {if (error) {throw error;} else {console. log ("file saved ");}});}
Error code: copyFile. js File
Var fs = require ("fs"); function readFile () {console. log ('-------- start reading file --------'); var fs = require ('fs'); fs.readFile('test.txt ', 'utf-8', function (err, data) {if (err) {console. log ("failed to read");} else {console. log (data); return data ;}}); console. log ('-------- read end --------');} function writeFile (data) {fs. writeFile ("test2.txt", data, function (error) {if (error) {throw error;} else {console. log ("file saved") ;}}) ;}function copyFile () {var txt = readFile (); writeFile (txt) ;}copyfile ();
The result of running node copyFile. js on the terminal is as follows:
Note:
1. file encoding. At the beginning, I directly created a local txt file. when reading the file, I found that the result was like aaaaaaa. The result showed that the file was garbled during editing and opening, second, it is best to include encoding, otherwise it will read according to the buffer.
2. Synchronization execution problems.
Undefined is the result of explain.
The correct method should be to write data to the file after reading:
Var fs = require ("fs"); function copyFile () {console. log ('-------- start reading file --------'); var fs = require ('fs'); fs.readFile('test.txt ', 'utf-8', function (err, data) {if (err) {console. log ("failed to read");} else {writeFile (data) return data ;}}); console. log ('-------- read ended --------');} function writeFile (data) {console. log (data); fs. writeFile ("test2.txt", data, 'utf8', function (error) {if (error) {throw error;} else {console. log ("file saved") ;}}) ;}copyfile ();
The above is all the content of this article, hoping to help you learn.
Articles you may be interested in:
- Use DOM in js to copy (clone) the node name data to the new XML file.
- Web page source code protection (prohibit right-click, copy, save as, view source files)
- How to copy or paste local files to a remote host through Remote Desktop Connection
- Use the xcopy command to copy local files to a remote server
- C # display the file copy progress bar instance based on the file size
- Java reads an excel file and copies it to a specified directory.
- Implementation of file replication methods and instances in Node. js
- How to delete, move, and copy files using JavaScript