简体   繁体   English

使用 vscode 扩展名 api 将数据读取和写入 a.txt 文件

[英]Reading & writing data to a .txt file using vscode extension api

So I am new to this vscode extension api. I have this functionality where I need to take input from the user when they click on certain line and then get the 1). input value, line number and file name所以我是这个 vscode 扩展 api 的新手。我有这个功能,我需要在用户单击特定行然后获取1). input value, line number and file name 1). input value, line number and file name and 2). store it to a text file 1). input value, line number and file name以及2). store it to a text file 2). store it to a text file . 2). store it to a text file中。

I am done with the first part, I am getting the data everything.我完成了第一部分,我正在获取所有数据。 Now I have to just write it to the file and if there is data already, new data should be appended not overwritten.现在我只需要将它写入文件,如果已经有数据,新数据应该被附加而不是被覆盖。

I have tried using fs.writeFileSync(filePath, data) and readFileSync but nothing, I do not know if I am doing it correctly.我试过使用fs.writeFileSync(filePath, data) and readFileSync但什么也没有,我不知道我是否做对了。 If someone can point me in the right direction I am just blank at this stage?如果有人能指出我正确的方向,我在这个阶段只是一片空白?

Any help would be appreciated, Thanks in advance.任何帮助将不胜感激,在此先感谢。

The FS node module works fine in an extension. FS 节点模块在扩展中工作正常。 I use it all the time for file work in my extension .我一直使用它来处理我的扩展程序中的文件工作。 Here's a helper function to export something to a file with error handling:这是一个 helper function 将一些东西导出到一个带有错误处理的文件:

/**
 * Asks the user for a file to store the given data in. Checks if the file already exists and ask for permission to
 * overwrite it, if so. Also copies a number extra files to the target folder.
 *
 * @param fileName A default file name the user can change, if wanted.
 * @param filter The file type filter as used in showSaveDialog.
 * @param data The data to write.
 * @param extraFiles Files to copy to the target folder (e.g. css).
 */
public static exportDataWithConfirmation(fileName: string, filters: { [name: string]: string[] }, data: string,
    extraFiles: string[]): void {
    void window.showSaveDialog({
        defaultUri: Uri.file(fileName),
        filters,
    }).then((uri: Uri | undefined) => {
        if (uri) {
            const value = uri.fsPath;
            fs.writeFile(value, data, (error) => {
                if (error) {
                    void window.showErrorMessage("Could not write to file: " + value + ": " + error.message);
                } else {
                    this.copyFilesIfNewer(extraFiles, path.dirname(value));
                    void window.showInformationMessage("Diagram successfully written to file '" + value + "'.");
                }
            });
        }
    });
}

And here an example where I read a file without user intervention :这是一个我在没有用户干预的情况下读取文件的示例:

    this.configurationDone.wait(1000).then(() => {
        ...
        try {
            const testInput = fs.readFileSync(args.input, { encoding: "utf8" });
            ...

        } catch (e) {
            ...
        }
        ...
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM