谁能告诉我如何读写文本文件(即 typescript 中的文件处理)的基本语法。如果有人能给我相应的链接,我将不胜感激。
最佳答案
在你的类中有一个方法readTextFile
。这完全基于 Javascript
readTextFile(file) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
console.log(allText); // Here is the contents of the file
}
}
}
rawFile.send(null);
}
然后使用它的路径调用该函数
this.readTextFile(path_of_the_file_to_be_read_as_string);
在上面的行中,this
基于您调用该函数的位置。
我们可以使用 JavaScript 读取文件,但无法写入文件。 https://stackoverflow.com/a/21012689/6554634
关于javascript - 文件处理 typescript Angular 2 的基本语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44644901/