Last active
September 2, 2017 12:42
-
-
Save ApsarasX/2f67b2a47d02e3d50fc9739b4e97f974 to your computer and use it in GitHub Desktop.
Node Learning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
function copy(src, dst) { | |
//小文件拷贝 | |
fs.writeFileSync(dst,fs.readFileSync(src)); | |
//大文件拷贝 | |
//fs.createReadStream(src).pipe(fs.createWriteStream(dst)); | |
} | |
function main(argv) { | |
copy(argv[0], argv[1]); | |
} | |
main(process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//实现大文件拷贝时候的stream的pipe操作 | |
const fs = require('fs'); | |
function pipe(rs,ws) { | |
rs.on('data',chunk=>{ | |
!ws.write(chunk) && rs.pause(); | |
}); | |
rs.on('end',()=>{ | |
ws.end(); | |
}); | |
ws.on('drain',()=>{ | |
rs.resume(); | |
}); | |
} | |
pipe(fs.createReadStream('./app.js'), fs.createWriteStream('./app.js.bak')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment