typescript读写文件

读写文件需要用到fs-extra和path模块

安装读写文件对应模块

$ npm install fs-extra -S
npm WARN saveError ENOENT: no such file or directory, open 'E:\study\ts\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'E:\study\ts\package.json'
npm WARN ts No description
npm WARN ts No repository field.
npm WARN ts No README data
npm WARN ts No license field.

+ fs-extra@8.1.0
added 4 packages from 2 contributors and audited 5 packages in 15.118s
found 0 vulnerabilities



$ npm install path -S
npm WARN saveError ENOENT: no such file or directory, open 'E:\study\ts\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'E:\study\ts\package.json'
npm WARN ts No description
npm WARN ts No repository field.
npm WARN ts No README data
npm WARN ts No license field.

+ path@0.12.7
added 4 packages from 2 contributors and audited 14 packages in 13.962s
found 0 vulnerabilities


如果想在vscode中添加自动提示功能,比如添加fs-extra模块的文档,则运行

npm i @types/fs-extra

然后鼠标定位到要查找的代码上,F12就跳转过去查看文档了。happy.

写代码列出当paths目录下文件列表

import * as fs from 'fs-extra';
import * as path from 'path';


const dirPath = path.resolve(`$(__dirname)`, '../paths/');
console.log(dirPath);

fs.readdir(dirPath, function (err, files) {
    if (err) {
        return console.log("err" + err);
    }

    files.forEach(function (f: string) {
        console.log("f-", f);
    });
});

列出列表结果为:

 

再扩展一下,读取paths文件夹下文件的内容。

import * as fs from 'fs-extra';
import * as path from 'path';


const dirPath = path.resolve(`$(__dirname)`, '../paths/');
const outDir = path.resolve(`$(__dirname)`,'../outDir/');
console.log(dirPath);

function mkOutDir(){
    
}

function readContent(dir: string, fName: string) {
    let fPath: string = dir.concat("\\").concat(fName);
    fs.readFile(fPath, "utf8", function (err, data) {
        if (err) {
            return console.error(err);
        }
        console.log(data);
        console.log("--------", fPath);
    });
}

fs.readdir(dirPath, function (err, files) {
    if (err) {
        return console.log("err" + err);
    }

    files.forEach(function (f: string) {
        readContent(dirPath, f);
    });
});

再扩展一下,读取paths目录下的文件,处理后写到outDir目录下

import * as fs from 'fs-extra';
import * as path from 'path';


const dirPath = path.resolve(`$(__dirname)`, '../paths/');
const outDir = path.resolve(`$(__dirname)`, '../outDir/');
console.log(dirPath);

function mkOutDir() {

}

function num2str(num: number): string {
    let text: string = String(num);
    if (text.indexOf(".") === -1) {
        text += ".00";
    } else {
        text += "0000";
    }

    let index: number = text.indexOf(".") + 3;
    text = text.substring(0, index);
    return text;
}

function dealOneFile(content: string, fName: string) {
    let sp: string[] = content.split("\n");
    if (sp.length <= 1) {
        console.error("error dealOneFile. fName = ", fName);
        process.exit(1);
    }

    let buf: string[] = [];
    let total: number = Number(sp[0]);
    let startSec: number = total;
    for (let i: number = 1; i < sp.length; i++) {
        let text: string = sp[i];
        if (text && text.length > 0) {
            let params: string[] = text.split("|");
            let begin: number = Number(params[1]);
            if (begin < startSec) {
                startSec = begin;
            }
        }
    }

    buf.push(String(total - startSec));
    for (let i: number = 1; i < sp.length; i++) {
        let text: string = sp[i];
        if (text && text.length > 0) {

            let params: string[] = text.split("|");
            let begin: number = Number(params[1]);
            let end: number = Number(params[2]);

            let bStr: string = num2str(begin - startSec);
            let eStr: string = num2str(end - startSec);
            let line: string = params[0] + "|" + bStr + "|" + eStr + "|" + params[3];
            buf.push(line);
        }
    }

    let fPath: string = outDir + "\\" + fName;
    fs.removeSync(fPath);

    fs.writeFileSync(fPath, buf.join("\n"));
    console.log("write fPath = ", fPath);
}

function readContent(dir: string, fName: string) {
    let fPath: string = dir.concat("\\").concat(fName);
    fs.readFile(fPath, "utf8", function (err, data) {
        if (err) {
            return console.error(err);
        }

        dealOneFile(data, fName);
    });
}

function dealFilesInDir() {
    fs.readdir(dirPath, function (err, files) {
        if (err) {
            return console.log("err" + err);
        }

        files.forEach(function (f: string) {
            readContent(dirPath, f);
        });
    });
}


let isOutExist: boolean = fs.pathExistsSync(outDir);
if (!isOutExist) {
    console.log("isOutExist = ", isOutExist, ", outDir=", outDir);
    fs.mkdirsSync(outDir);
}

dealFilesInDir();








处理前的数据:

104.47
1104|20.00|73.00|89
1104|20.50|73.50|89
1104|21.00|74.00|89
1104|21.50|74.50|89
1104|22.00|75.00|89
1104|22.50|75.50|89
1104|23.00|76.00|89
1104|23.50|76.50|89
1104|24.00|77.00|89
1104|24.50|77.50|89
1104|25.00|78.00|89
1104|25.50|78.50|89
1104|26.00|79.00|89
1104|26.50|79.50|89
1104|27.00|80.00|89
1104|27.50|80.50|89
1104|28.00|81.00|89
1104|28.50|81.50|89
1104|29.00|82.00|89
1104|29.50|82.50|89
1104|30.00|83.00|89
1104|30.50|83.50|89
1104|31.00|84.00|89
1104|31.50|84.50|89
1104|32.00|85.00|89
1104|32.50|85.50|89
1104|33.00|86.00|89
1104|33.50|86.50|89
1104|34.00|87.00|89
1104|34.50|87.50|89
1104|35.00|88.00|89
1104|35.50|88.50|89
1104|36.00|89.00|89
1104|36.50|89.50|89
1104|37.00|90.00|89
1104|37.50|90.50|89
1104|38.00|91.00|89
1104|38.50|91.50|89
1104|39.00|92.00|89
1104|39.50|92.50|89
1104|40.00|93.00|89
1104|40.50|93.50|89
1104|41.00|94.00|89
1104|41.50|94.50|89
1104|42.00|95.00|89
1104|42.50|95.50|89
1104|43.00|96.00|89
1104|43.50|96.50|89
1104|44.00|97.00|89
1104|44.50|97.50|89
1104|45.00|98.00|89
1104|20.00|72.69|90
1104|20.50|73.19|90
1104|21.00|73.69|90
1104|21.50|74.19|90
1104|22.00|74.69|90
1104|22.50|75.19|90
1104|23.00|75.69|90
1104|23.50|76.19|90
1104|24.00|76.69|90
1104|24.50|77.19|90
1104|25.00|77.69|90
1104|25.50|78.19|90
1104|26.00|78.69|90
1104|26.50|79.19|90
1104|27.00|79.69|90
1104|27.50|80.19|90
1104|28.00|80.69|90
1104|28.50|81.19|90
1104|29.00|81.69|90
1104|29.50|82.19|90
1104|30.00|82.69|90
1104|30.50|83.19|90
1104|31.00|83.69|90
1104|31.50|84.19|90
1104|32.00|84.69|90
1104|32.50|85.19|90
1104|33.00|85.69|90
1104|33.50|86.19|90
1104|34.00|86.69|90
1104|34.50|87.19|90
1104|35.00|87.69|90
1104|35.50|88.19|90
1104|36.00|88.69|90
1104|36.50|89.19|90
1104|37.00|89.69|90
1104|37.50|90.19|90
1104|38.00|90.69|90
1104|38.50|91.19|90
1104|39.00|91.69|90
1104|39.50|92.19|90
1104|40.00|92.69|90
1104|40.50|93.19|90
1104|41.00|93.69|90
1104|41.50|94.19|90
1104|42.00|94.69|90
1104|42.50|95.19|90
1104|43.00|95.69|90
1104|43.50|96.19|90
1104|44.00|96.69|90
1104|44.50|97.19|90
1104|45.00|97.69|90
1210|20.00|104.47|91
1210|20.00|104.44|92
1101|20.00|72.92|122
1101|20.50|73.42|122
1101|21.00|73.92|122
1101|21.50|74.42|122
1101|22.00|74.92|122
1101|22.50|75.42|122
1101|23.00|75.92|122
1101|23.50|76.42|122
1101|24.00|76.92|122
1101|24.50|77.42|122
1101|25.00|77.92|122
1101|25.50|78.42|122
1101|26.00|78.92|122
1101|26.50|79.42|122
1101|27.00|79.92|122
1101|27.50|80.42|122
1101|28.00|80.92|122
1101|28.50|81.42|122
1101|29.00|81.92|122
1101|29.50|82.42|122
1101|30.00|82.92|122
1101|30.50|83.42|122
1101|31.00|83.92|122
1101|31.50|84.42|122
1101|32.00|84.92|122
1101|32.50|85.42|122
1101|33.00|85.92|122
1101|33.50|86.42|122
1101|34.00|86.92|122
1101|34.50|87.42|122
1101|35.00|87.92|122
1101|35.50|88.42|122
1101|36.00|88.92|122
1101|36.50|89.42|122
1101|37.00|89.92|122
1101|37.50|90.42|122
1101|38.00|90.92|122
1101|38.50|91.42|122
1101|39.00|91.92|122
1101|39.50|92.42|122
1101|40.00|92.92|122
1101|40.50|93.42|122
1101|41.00|93.92|122
1101|41.50|94.42|122
1101|42.00|94.92|122
1101|42.50|95.42|122
1101|43.00|95.92|122
1101|43.50|96.42|122
1101|44.00|96.92|122
1101|44.50|97.42|122
1101|45.00|97.92|122
1101|20.00|72.92|123
1101|20.50|73.42|123
1101|21.00|73.92|123
1101|21.50|74.42|123
1101|22.00|74.92|123
1101|22.50|75.42|123
1101|23.00|75.92|123
1101|23.50|76.42|123
1101|24.00|76.92|123
1101|24.50|77.42|123
1101|25.00|77.92|123
1101|25.50|78.42|123
1101|26.00|78.92|123
1101|26.50|79.42|123
1101|27.00|79.92|123
1101|27.50|80.42|123
1101|28.00|80.92|123
1101|28.50|81.42|123
1101|29.00|81.92|123
1101|29.50|82.42|123
1101|30.00|82.92|123
1101|30.50|83.42|123
1101|31.00|83.92|123
1101|31.50|84.42|123
1101|32.00|84.92|123
1101|32.50|85.42|123
1101|33.00|85.92|123
1101|33.50|86.42|123
1101|34.00|86.92|123
1101|34.50|87.42|123
1101|35.00|87.92|123
1101|35.50|88.42|123
1101|36.00|88.92|123
1101|36.50|89.42|123
1101|37.00|89.92|123
1101|37.50|90.42|123
1101|38.00|90.92|123
1101|38.50|91.42|123
1101|39.00|91.92|123
1101|39.50|92.42|123
1101|40.00|92.92|123
1101|40.50|93.42|123
1101|41.00|93.92|123
1101|41.50|94.42|123
1101|42.00|94.92|123
1101|42.50|95.42|123
1101|43.00|95.92|123
1101|43.50|96.42|123
1101|44.00|96.92|123
1101|44.50|97.42|123
1101|45.00|97.92|123

处理后的数据

84.47
1104|0.00|53.00|89
1104|0.50|53.50|89
1104|1.00|54.00|89
1104|1.50|54.50|89
1104|2.00|55.00|89
1104|2.50|55.50|89
1104|3.00|56.00|89
1104|3.50|56.50|89
1104|4.00|57.00|89
1104|4.50|57.50|89
1104|5.00|58.00|89
1104|5.50|58.50|89
1104|6.00|59.00|89
1104|6.50|59.50|89
1104|7.00|60.00|89
1104|7.50|60.50|89
1104|8.00|61.00|89
1104|8.50|61.50|89
1104|9.00|62.00|89
1104|9.50|62.50|89
1104|10.00|63.00|89
1104|10.50|63.50|89
1104|11.00|64.00|89
1104|11.50|64.50|89
1104|12.00|65.00|89
1104|12.50|65.50|89
1104|13.00|66.00|89
1104|13.50|66.50|89
1104|14.00|67.00|89
1104|14.50|67.50|89
1104|15.00|68.00|89
1104|15.50|68.50|89
1104|16.00|69.00|89
1104|16.50|69.50|89
1104|17.00|70.00|89
1104|17.50|70.50|89
1104|18.00|71.00|89
1104|18.50|71.50|89
1104|19.00|72.00|89
1104|19.50|72.50|89
1104|20.00|73.00|89
1104|20.50|73.50|89
1104|21.00|74.00|89
1104|21.50|74.50|89
1104|22.00|75.00|89
1104|22.50|75.50|89
1104|23.00|76.00|89
1104|23.50|76.50|89
1104|24.00|77.00|89
1104|24.50|77.50|89
1104|25.00|78.00|89
1104|0.00|52.69|90
1104|0.50|53.19|90
1104|1.00|53.69|90
1104|1.50|54.19|90
1104|2.00|54.69|90
1104|2.50|55.19|90
1104|3.00|55.69|90
1104|3.50|56.19|90
1104|4.00|56.69|90
1104|4.50|57.19|90
1104|5.00|57.69|90
1104|5.50|58.19|90
1104|6.00|58.69|90
1104|6.50|59.19|90
1104|7.00|59.69|90
1104|7.50|60.19|90
1104|8.00|60.69|90
1104|8.50|61.19|90
1104|9.00|61.69|90
1104|9.50|62.19|90
1104|10.00|62.69|90
1104|10.50|63.19|90
1104|11.00|63.69|90
1104|11.50|64.19|90
1104|12.00|64.69|90
1104|12.50|65.19|90
1104|13.00|65.69|90
1104|13.50|66.19|90
1104|14.00|66.69|90
1104|14.50|67.19|90
1104|15.00|67.69|90
1104|15.50|68.19|90
1104|16.00|68.69|90
1104|16.50|69.19|90
1104|17.00|69.69|90
1104|17.50|70.19|90
1104|18.00|70.69|90
1104|18.50|71.19|90
1104|19.00|71.69|90
1104|19.50|72.19|90
1104|20.00|72.69|90
1104|20.50|73.19|90
1104|21.00|73.69|90
1104|21.50|74.19|90
1104|22.00|74.69|90
1104|22.50|75.19|90
1104|23.00|75.69|90
1104|23.50|76.19|90
1104|24.00|76.69|90
1104|24.50|77.19|90
1104|25.00|77.69|90
1210|0.00|84.47|91
1210|0.00|84.44|92
1101|0.00|52.92|122
1101|0.50|53.42|122
1101|1.00|53.92|122
1101|1.50|54.42|122
1101|2.00|54.92|122
1101|2.50|55.42|122
1101|3.00|55.92|122
1101|3.50|56.42|122
1101|4.00|56.92|122
1101|4.50|57.42|122
1101|5.00|57.92|122
1101|5.50|58.42|122
1101|6.00|58.92|122
1101|6.50|59.42|122
1101|7.00|59.92|122
1101|7.50|60.42|122
1101|8.00|60.92|122
1101|8.50|61.42|122
1101|9.00|61.92|122
1101|9.50|62.42|122
1101|10.00|62.92|122
1101|10.50|63.42|122
1101|11.00|63.92|122
1101|11.50|64.42|122
1101|12.00|64.92|122
1101|12.50|65.42|122
1101|13.00|65.92|122
1101|13.50|66.42|122
1101|14.00|66.92|122
1101|14.50|67.42|122
1101|15.00|67.92|122
1101|15.50|68.42|122
1101|16.00|68.92|122
1101|16.50|69.42|122
1101|17.00|69.92|122
1101|17.50|70.42|122
1101|18.00|70.92|122
1101|18.50|71.42|122
1101|19.00|71.92|122
1101|19.50|72.42|122
1101|20.00|72.92|122
1101|20.50|73.42|122
1101|21.00|73.92|122
1101|21.50|74.42|122
1101|22.00|74.92|122
1101|22.50|75.42|122
1101|23.00|75.92|122
1101|23.50|76.42|122
1101|24.00|76.92|122
1101|24.50|77.42|122
1101|25.00|77.92|122
1101|0.00|52.92|123
1101|0.50|53.42|123
1101|1.00|53.92|123
1101|1.50|54.42|123
1101|2.00|54.92|123
1101|2.50|55.42|123
1101|3.00|55.92|123
1101|3.50|56.42|123
1101|4.00|56.92|123
1101|4.50|57.42|123
1101|5.00|57.92|123
1101|5.50|58.42|123
1101|6.00|58.92|123
1101|6.50|59.42|123
1101|7.00|59.92|123
1101|7.50|60.42|123
1101|8.00|60.92|123
1101|8.50|61.42|123
1101|9.00|61.92|123
1101|9.50|62.42|123
1101|10.00|62.92|123
1101|10.50|63.42|123
1101|11.00|63.92|123
1101|11.50|64.42|123
1101|12.00|64.92|123
1101|12.50|65.42|123
1101|13.00|65.92|123
1101|13.50|66.42|123
1101|14.00|66.92|123
1101|14.50|67.42|123
1101|15.00|67.92|123
1101|15.50|68.42|123
1101|16.00|68.92|123
1101|16.50|69.42|123
1101|17.00|69.92|123
1101|17.50|70.42|123
1101|18.00|70.92|123
1101|18.50|71.42|123
1101|19.00|71.92|123
1101|19.50|72.42|123
1101|20.00|72.92|123
1101|20.50|73.42|123
1101|21.00|73.92|123
1101|21.50|74.42|123
1101|22.00|74.92|123
1101|22.50|75.42|123
1101|23.00|75.92|123
1101|23.50|76.42|123
1101|24.00|76.92|123
1101|24.50|77.42|123
1101|25.00|77.92|123

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值