读写打字稿中的文本文件 [英] Read and write a text file in typescript
问题描述
我应该如何在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屋!