Home  >  Article  >  Web Front-end  >  How to copy files in Nodejs_node.js

How to copy files in Nodejs_node.js

WBOY
WBOYOriginal
2016-05-16 15:11:141483browse

Every front-end kid knows that JavaScript does not have permission to operate disk files, and server kids have always despised it. But nodejs can be said to make our front-end proud. I have been learning node recently, and its powerful functions make people very excited and excited. Today I learned how it reads and writes files.

First you need to introduce the fs module, which comes with nodejs.

var fs=require("fs");
For details, please refer to Nodejs API: http://www.w3cfuns.com/tools.php?mod=booknodejs

There are two main methods used:

1. fs.readFile(filename, [encoding], [callback])

This is asynchronous reading of files, filename is the file path, encoding is the encoding format, and callback is the callback function.

Asynchronously read all the contents of a file, the example 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('--------开始读取文件--------');
  var fs = require('fs');
  fs.readFile('test.txt', 'utf-8', function(err, data) {
    if (err) {
      console.log("读取失败");
    } else {
      console.log(data);
      return data;
    }
  });
  console.log('--------读取结束--------');
}

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("文件已保存");  
    }
  });
}

Error code: copyFile.js file

var fs=require("fs");
function readFile(){
  console.log('--------开始读取文件--------');
  var fs = require('fs');
  fs.readFile('test.txt', 'utf-8', function(err, data) {
    if (err) {
      console.log("读取失败");
    } else {
      console.log(data);
      return data;
    }
  });
  console.log('--------读取结束--------');
}

function writeFile(data){
  fs.writeFile("test2.txt",data,function(error){
    if(error){
      throw error;
    }else{
      console.log("文件已保存");  
    }
  });
}
function copyFile(){
  var txt=readFile();
  writeFile(txt);
}
copyFile();

The result of running node copyFile.js in the terminal is as follows:

Note:

1. File encoding. At the beginning, I directly created a new txt document locally. When I read it, I found that the result was always aaaaaaa. It turned out that when I opened it with editor, it was garbled. Secondly, it is best to bring encoding. , otherwise it will read according to the buffer.

2. Synchronous execution problem.

There is a problem with the above code. I wrote the file reading and writing methods separately. I originally wanted to copy the contents of the test.txt file to test2.txt, but reading the file is executed asynchronously. , that is to say, no one knows when it will be executed, so the result of test.txt is undefined.

The correct method should be to write the file after reading:

var fs=require("fs");
function copyFile(){
  console.log('--------开始读取文件--------');
  var fs = require('fs');
  fs.readFile('test.txt', 'utf-8', function(err, data) {
    if (err) {
      console.log("读取失败");
    } else {
      writeFile(data)
      return data;
    }
  });
  console.log('--------读取结束--------');
}

function writeFile(data){
  console.log(data);
  fs.writeFile("test2.txt",data,'utf8',function(error){
    if(error){
      throw error;
    }else{
      console.log("文件已保存");  
    }
  });
}

copyFile();

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn