NODEJS[06]:fs模块_文件读取
·
Yin灏
文件读取
方法 | 说明 |
---|---|
readFile | 异步读取 |
readFileSync | 同步读取 |
createReadStream | 流式读取 |
readFile 异步读取
fs.readFile("./file.txt", (err, data) => {
if (err) {
console.log("读取失败");
return;
}
console.log(data.toString());
});
readFileSync 同步读取
let data = fs.readFileSync("./file.txt");
console.log(data.toString());
createReadStream 流式读取
const rs = fs.createReadStream("../video.mp4");
rs.on("data", (chunk) => {
console.log(chunk.length);
});
rs.on("end", () => {
console.log("读取完成");
});