use nodejs read a fileand convert the String to a array
Here is an example code snippet in Node.js that reads a file and converts the String to an array:
const fs = require('fs');
fs.readFile('file.txt', 'utf8', function(err, data) {
if (err) throw err;
const dataArray = data.split('\n');
console.log(dataArray);
});
This code uses the fs module in Node.js to read a file named file.txt in the current working directory. The second argument 'utf8' specifies the encoding of the file to be read as a string.
The readFile function takes a callback function as its second argument, which is called once the file is read. In this callback function, the file data is split by the newline character \n using the split method, which returns an array of strings. This array is then logged to the console.
原文地址: http://www.cveoy.top/t/topic/bWxv 著作权归作者所有。请勿转载和采集!