Node.js WriteStream在关闭之前不会将数据写入文件 [英] Node.js WriteStream doesn't write data to file until closed

查看:330
本文介绍了Node.js WriteStream在关闭之前不会将数据写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当调用WriteStream.write()时,如何使数据写入文件?

事实证明,这在我使用REPL并调用该函数时有效.但是,这在我的程序中不起作用:

import * as FS from "fs";
import { LetterGroup } from "./LetterGroup";
import { Dictionary } from "./Dictionary";
import { Word } from "./Word";
import * as OS from "os";

const groups: Array<LetterGroup> = LetterGroup.letterGroupsFromString(FS.readFileSync("./letterGroups.txt").toString());
const dictionary = Dictionary.create(FS.readFileSync("./dictionary.txt").toString());

const inputStr: string = FS.readFileSync("./input.txt").toString();
const inputWords = new Array<Word>();
const fileStream = FS.createWriteStream("./output.txt");

for (const line of inputStr.trim().split(OS.EOL))
{
    inputWords.push(new Word(line));
}

function permute(index: number)
{
    index = Math.floor(index);

    if (!(index >= 0 && index < inputWords.length))
    {
        return;
    }

    const word: Word = inputWords[index];

    let outputString: string = "";

    outputString += `Valid permutations of '${word.wordString}':` + OS.EOL;

    console.log(`Testing '${ word.wordString }'...`);

    for (const permutation of word.generatePermutations(groups, dictionary, true))
    {
        outputString += permutation.wordString + OS.EOL;
    }

    outputString += OS.EOL;
    console.log();

    if (!fileStream.write(outputString))
    {
        console.log("Wrote to file too fast! Will resume writing once the system catches up...");
        fileStream.once("drain", () => permute(index));
        return;
    }

    permute(index + 1);
}

permute(0);

应注意,word.generatePermutations是一项非常密集的功能,通常需要几分钟(有时需要一个小时以上)才能返回.我的问题是,返回的数据应立即写入输出文件,以便整个程序无需完成运行即可读取结果.希望有人能理解所有这一切.

澄清一下,在REPL中写入WriteStream的数据会立即写入文件,而在整个程序完成运行之前,不会在我的程序中将数据写入文件.

解决方案

尝试以下软件包:

https://www.npmjs.com/package/flushwritable

https://www.npmjs.com/package/flush-write-stream

我想你的问题是重复的 如何正确刷新Node.js文件writeStream?

How can I cause the data to be written to the file as WriteStream.write() is called?

Edit: As it turns out, this works when I use the REPL and call the function. However, this doesn't work in my program:

import * as FS from "fs";
import { LetterGroup } from "./LetterGroup";
import { Dictionary } from "./Dictionary";
import { Word } from "./Word";
import * as OS from "os";

const groups: Array<LetterGroup> = LetterGroup.letterGroupsFromString(FS.readFileSync("./letterGroups.txt").toString());
const dictionary = Dictionary.create(FS.readFileSync("./dictionary.txt").toString());

const inputStr: string = FS.readFileSync("./input.txt").toString();
const inputWords = new Array<Word>();
const fileStream = FS.createWriteStream("./output.txt");

for (const line of inputStr.trim().split(OS.EOL))
{
    inputWords.push(new Word(line));
}

function permute(index: number)
{
    index = Math.floor(index);

    if (!(index >= 0 && index < inputWords.length))
    {
        return;
    }

    const word: Word = inputWords[index];

    let outputString: string = "";

    outputString += `Valid permutations of '${word.wordString}':` + OS.EOL;

    console.log(`Testing '${ word.wordString }'...`);

    for (const permutation of word.generatePermutations(groups, dictionary, true))
    {
        outputString += permutation.wordString + OS.EOL;
    }

    outputString += OS.EOL;
    console.log();

    if (!fileStream.write(outputString))
    {
        console.log("Wrote to file too fast! Will resume writing once the system catches up...");
        fileStream.once("drain", () => permute(index));
        return;
    }

    permute(index + 1);
}

permute(0);

It should be noted that word.generatePermutations is an extremely intensive function and often can take several minutes (sometimes over an hour) to return. My issue is that the data it returns should be written to the output file immediately so that the entire program doesn't need to finish running for the results to be read. Hopefully someone can make sense of all this.

To clarify, data written to a WriteStream in the REPL is immediately written to the file, while no data is written to the file in my program until the entire program finishes running.

解决方案

Try these packages:

https://www.npmjs.com/package/flushwritable

https://www.npmjs.com/package/flush-write-stream

And I think you question is duplicate of How to flush Node.js file writeStream properly?

这篇关于Node.js WriteStream在关闭之前不会将数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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