使用fs.writeFile()写入文件

1.首先介绍fs.writeFile函数结构

fs.writeFile(file,data,callback)
file 文件名或文件描述
data 写入的数据
callback 回调函数

2.引用fs文件系统

let fs = require('fs');
	        let time = new Date();
            time.setMilliseconds(time.getMilliseconds()-1500);
            // 图表数据chartData
            let chartData = [[],[],[]];
            // 页面信息data
            let = {
            	today:"冒个泡",
            	maxArr:['Max',0,0,0],
            	minArr:['Min',0,0,0],
            	};
           // 定义变量 string  拼接数据
            let string = "\n";
            string += 'Time Stamp \n';
            string += time.toLocaleString()+'\n';
            string += data.today+'\n';
      

            //  温度信息展示
            string += ",Rotation/°,Extention/%,Bend/° \n";
            string += data.maxArr+'\n';
            string += data.minArr+'\n\n\n';
            // 图表数据信息
            string += "Scan time , Rotation,Extention,Bend \nSec,ANG°,Percent%,ANG°\n";
            if(chartData&&chartData[0]&&chartData[0].length>0){
                for(let i = 0; i<chartData[0].length; i++){
                    string += 0.0002*i+','+(chartData[0][i]||0)+','+(chartData[1][i]||0)+','+(chartData[2][i]||0)
                    +'\n';
                }
            }
	    //finalData 写入的数据 
	    //new Uint8Array()实例化8位无符号整型数组
		//Buffer.from创建对象实例
        const finalData = new Uint8Array(Buffer.from(string));
        //dir 文件目录 
        let dir = '../VascSF ';
        //检测文件目录existsSync
        if(!fs.existsSync(dir)){
        //创建文件目录
            fs.mkdirSync(dir);
        }
        
        fs.writeFile(dir+'/'+
            time.getFullYear() + '-' +
            (time.getMonth()+1) + '-' +
            time.getDate() + ' '+
            time.getHours() + '-' +
            time.getMinutes() + '-' +
            time.getSeconds() + '-' +
            time.getMilliseconds()
        +'.csv', finalData, (err) => {
        if (err) throw err;
        console.log('The file has been saved!');
        });
	

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值