fs是node安装自带的模块,无需另外安装,但是需要导入

const fs=require('fs')

const fs=require('fs');
/**
 * 文件基本操作
 */

//fd是文件的标识

//写入
fs.open('fileWrite.txt','w',(err,fd)=>{
      if( err ){
          console.log(err);
      }else{
        fs.write(fd,'今天的天气不太友好',(err,len,str)=>{
            console.log(len);
            console.log(str);
            fs.close(fd,(err)=>{
                if( err ){
                     console.log(err);
                }
            });
        });
      }
});

var fd=fs.openSync('fileWriteSync.txt','w');
fs.writeSync(fd,'今天是2021年4月3日');
fs.closeSync(fd);


fs.open('fileWrite.txt','r',(err,fd)=>{
    if( err ){
        throw err;
    }
    fs.read(fd,(err,len,data)=>{
        if( err ){
            throw err;
        }
        console.log(len);
        console.log(data.toString());
    });
});

//简单文件写入读取操作
fs.writeFile('writeFile.txt','hello WriteFile',(err)=>{
    if( err )
        throw err;
})

fs.readFile('writeFile.txt',(err,data)=>{
    if( err ){
        throw err;
    }
    console.log(data.toString());
});

var ws=fs.createWriteStream('writeStream.txt');

ws.once('open',()=>{
    console.log('开始流入');
})
ws.once('close',()=>{
    console.log('关闭了');
})

ws.write('gwrg');
ws.write('发个个人');
ws.write('服务人跟帖');
ws.write('富翁服务日');
ws.write('gwr富翁服务日g');
ws.write('是微软不玩儿吧');

ws.end();


var rs=fs.createReadStream('writeStream.txt');
rs.once('open',()=>{
    console.log('文件开始读取了');
});

rs.once('close',()=>{
    console.log('文件关闭了读取');
});

rs.on('data',(data)=>{
    console.log(data);
})

//rs.pipe(ws);