We‘ll read a csv file in node.js both synchronously, and asynchronously. The file we‘re reading is a plain text, utf8 file - but you can also use fs.readFile
to read a binary file as a buffer. We‘ll look at the differences between readFile
and readFileSync
, and show examples of how to catch errors if they occur.
const fs = require(‘fs‘) // Async: fs.readFile(‘data.csv‘, ‘utf8‘, (err, data) => { console.log(data) }) // Sync: let results try { // (invalid file error example) const data = fs.readFileSync(‘nofile.csv‘, ‘utf8‘) results = data } catch(e) { console.log("error", e) } console.log("results", results)
原文地址:https://www.cnblogs.com/Answer1215/p/9823937.html
时间: 2024-10-09 03:12:58