读写打字稿中的文本文件 [英] Read and write a text file in typescript

查看:374
本文介绍了读写打字稿中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何在node.js中从打字稿读取和写入文本文件? 我不确定是否可以在node.js中对文件进行读/写操作,如果没有,我相信应该有一种访问文件系统的方法.

How should I read and write a text file from typescript in node.js? I am not sure would read/write a file be sandboxed in node.js, if not, i believe there should be a way in accessing file system.

推荐答案

相信应该有一种访问文件系统的方法.

believe there should be a way in accessing file system.

使用npm i @types/node包括node.d.ts.然后创建一个新的tsconfig.json文件(npx tsc --init)并创建一个.ts文件,如下所示:

Include node.d.ts using npm i @types/node. And then create a new tsconfig.json file (npx tsc --init) and create a .ts file as followed:

import fs from 'fs';
fs.readFileSync('foo.txt','utf8');

您还可以在fs中使用其他功能: https://nodejs.org/api/fs.html

You can use other functions in fs as well : https://nodejs.org/api/fs.html

节点快速入门: https://basarat.gitbooks.io/typescript/content/docs/node/nodejs.html

这篇关于读写打字稿中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆