2016-11-25 64 views

回答

0
function (file, callback) { 
    fs.readFile(file, (err, 'utf8', data) => { 
     if (err) return callback(err); 

     var lines = data.split('\n'); 

     fs.open(file, 'w', (err, fd) => { 
      if (err) return callback(err) 

      lines.forEach(line => { 
       if (line === 'meet your condition') { 
        // do your write using fs.write(fd,) 
       } 
      }) 
      callback(); 
     }) 
    }) 
} 
0

在fs的帮助下使用节点fs模块,你可以异步和同步地执行操作。下面是作为异步

function readWriteData(savPath, srcPath) { 
    fs.readFile(srcPath, 'utf8', function (err, data) { 
      if (err) throw err; 
      //Do your processing, MD5, send a satellite to the moon or can add conditions , etc. 
      fs.writeFile (savPath, data, function(err) { 
       if (err) throw err; 
       console.log('complete'); 
      }); 
     }); 
} 

同步方式示例的示例

function readFileContent(srcPath, callback) { 
    fs.readFile(srcPath, 'utf8', function (err, data) { 
     if (err) throw err; 
     callback(data); 
     } 
    ); 
} 

function writeFileContent(savPath, srcPath) { 
    readFileContent(srcPath, function(data) { 
     fs.writeFile (savPath, data, function(err) { 
      if (err) throw err; 
      console.log('complete'); 
     }); 
    }); 
} 
相关问题