Node.js/stream:修订间差异
来自康健生活
| 第114行: | 第114行: | ||
fs.writeFile(path.resolve(target), data, (err) => {}); | fs.writeFile(path.resolve(target), data, (err) => {}); | ||
}) | }) | ||
};</syntaxhighlight> | |||
通过创建流进行边读边写。 | |||
<syntaxhighlight lang="javascript"> | |||
const fs = require('fs'); | |||
const path = require('path'); | |||
const read2write = (source, target) => { | |||
const rs = fs.createReadStream(source); | |||
const ws = fs.createWriteStream(target); | |||
rs.on('data', data => { | |||
ws.write(data); | |||
}); | |||
rs.on('end', () => { | |||
ws.end(); | |||
}); | |||
};</syntaxhighlight> | };</syntaxhighlight> | ||