Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从文件中读取行不会';t使用fs.readFileSync返回正确的字符串_Javascript_Node.js_Readfile - Fatal编程技术网

Javascript 从文件中读取行不会';t使用fs.readFileSync返回正确的字符串

Javascript 从文件中读取行不会';t使用fs.readFileSync返回正确的字符串,javascript,node.js,readfile,Javascript,Node.js,Readfile,用下面的代码逐行读取文件时。它似乎没有正确读取字符串 文件中每行的字符串部分如下: b53pd4574z8pe9x793go console.log(pathcreatefile)正确显示: b53pd4574z8pe9x793go 但似乎是:fs.promises.writeFile做到了这一点?: b53'd4574z8pe9x793go 控制台错误如下: (节点:1148)未经处理的PromisejectionWarning:错误:eNote:没有此类文件或目录,请打开'C:\myproj

用下面的代码逐行读取文件时。它似乎没有正确读取字符串

文件中每行的字符串部分如下:
b53pd4574z8pe9x793go

console.log(pathcreatefile)正确显示:
b53pd4574z8pe9x793go

但似乎是:fs.promises.writeFile做到了这一点?:
b53'd4574z8pe9x793go

控制台错误如下:
(节点:1148)未经处理的PromisejectionWarning:错误:eNote:没有此类文件或目录,请打开'C:\myproject\instances\b53'd4574z8pe9x793go\folder\testA.txt

我的代码如下。我在代码中添加了从文件中读取的3行:

'use strict';
const fs = require('fs');
var i;


//1|one/a|test1|C:/myproject/instances/b53pd4574z8pe9x793go/folder/testA.txt
//1|two/b|test2|C:/myproject/instances/b53pd4574z8pe9x793go/folder/testB.txt
//1|three/c|test3|C:/myproject/instances/b53pd4574z8pe9x793go/folder/testC.txt
var textarray = fs.readFileSync("C:/myproject/folder/testfile.txt").toString('utf-8').split("\n"); //Read the file


(async () => {
    var thePromises = []
    for (i = 0; i < textarray.length; i++) {

        //1|one/a|test1|C:/myproject/instances/b53pd4574z8pe9x793go/folder/testA.txt
        const line = textarray[i].split("|")
        if (line.length == 4) {

            const pathcreatefile = line[3] //C:/myproject/instances/b53pd4574z8pe9x793go/folder/testA.txt
            console.log(pathcreatefile)

            try {
                    let tickerProcessing = new Promise(async (resolve) => {
                    await fs.promises.writeFile(pathcreatefile, "hello")
                    resolve()
                })
                thePromises.push(tickerProcessing)

            } catch (e) {
                console.error(e)
            }
        }
    }

    // wait for all of them to execute or fail
    await Promise.all(thePromises)
    })()
“严格使用”;
常数fs=要求('fs');
var i;
//1 | one/a | test1 | C:/myproject/instances/b53pd4574z8pe9x793go/folder/testA.txt
//1 |两个/b |测试2 | C:/myproject/instances/b53pd4574z8pe9x793go/folder/testB.txt
//1 |三个/c | test3 | c:/myproject/instances/b53pd4574z8pe9x793go/folder/testC.txt
var textarray=fs.readFileSync(“C:/myproject/folder/testfile.txt”).toString('utf-8').split(“\n”)//读文件
(异步()=>{
var thePromises=[]
对于(i=0;i{
等待fs.promises.writeFile(路径创建文件,“hello”)
解决()
})
Promises.push(票证处理)
}捕获(e){
控制台错误(e)
}
}
}
//等待它们全部执行或失败
等待承诺。所有(提议)
})()

无需将fs.promises.writeFile包装成一个额外的Promise,它返回的Promise没有任何包装

此外,还应该从“os”包中为行分隔符添加常量,以使其在不同的操作系统中工作。 以下代码适用于您:

         'use strict';
var endOfLine = require('os').EOL;
const fs = require('fs');
var textarray = fs.readFileSync("./testfile.txt").toString('utf-8').split(endOfLine);
(async () => {
    await Promise.all(textarray.map((textElement) => {
        const line = textElement.split("|")
        if (line.length === 4) {
            const pathcreatefile = line[3] 
            console.log(pathcreatefile)
            return fs.promises.writeFile(pathcreatefile, "hello")
        }
    }));
})()

您是否尝试更改编码选项?像
fs.promises.writeFile(pathcreatefile,“hello”,“utf8”)
?这不起作用。必须使用Is:var endOfLine=require('os').EOL;如答案所示。您应该提供错误的解释,而不仅仅是一个片段,谢谢您:var endOfLine=require('os').EOL;正如我所看到的那样,这使它发挥了作用。很高兴知道我也能写这样的承诺。