Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想使用 Node.js 读取文本文件 (.txt)。我需要将每个文本的行推入数组,如下所示:
a b c
到
var array = ['a', 'b', 'c'];
我怎样才能做到这一点?
你可以这样做 :
var fs = require("fs"); var array = fs.readFileSync(path).toString().split('\n');
或异步变体:
var fs = require("fs"); fs.readFile(path, function(err, f){ var array = f.toString().split('\n'); // use the array });