1、node.js中文件读取流方式:
var fs=require("fs");
var data;
var s1=fs.createReadStream("input.txt");
s1.on("data",function(txt){
data+=txt;
})
s1.on("end",function(){
console.log("文件流式读取成功!")
})
2、管道方式:
var fs=require("fs");
var s1=fs.createReadStream("input.txt");
var s2=fs.createWriteStream("output.txt");
s1.pipe(s2)
3、获取请求的网站信息:
var https=require("https");
var fs=require("fs");
https.get("https://www.baidu.com/",function(res){
var data;
res.on("data",function(txt){
data+=txt;
})
res.on("end",function(){
//console.log(data)
fs.writeFile("input.txt",data,function(){
console.log("文件写入完成!")
})
})
时间: 2024-10-11 22:24:41