1234567891011121314151617181920212223242526272829303132333435 |
- /**
- * Created by Tony on 2017/4/10.
- */
- let fs = require('fs');
- module.exports = {
- writeArrayToFile: function(arr, filePath) {
- if (arr && filePath && Array.isArray(arr)) {
- let chunks = [], len = 0;
- for (let i = 0; i < arr.length; i++) {
- let buffer = new Buffer(arr[i]);
- chunks.push(buffer);
- len += buffer.length;
- //
- }
- let resultBuffer = new Buffer(len);
- for(let i=0,size=chunks.length,pos=0;i<size;i++){
- chunks[i].copy(resultBuffer,pos);
- pos += chunks[i].length;
- }
- fs.writeFile(filePath, resultBuffer, function(err){
- if(err) throw err;
- //console.log('Write file: ' + filePath + ' ok!');
- });
- }
- },
- writeObjToFile: function(obj, filePath) {
- if (obj) {
- let arr = [];
- arr.push(JSON.stringify(obj));
- this.writeArrayToFile(arr, filePath);
- }
- }
- };
|