[Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync
2023-09-14 09:00:49 时间
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)
相关文章
- node.js 安装与环境变量配置
- js操作日期基本格式
- node.js异步请求大坑
- js手写前端需要掌握的点
- 图解 Node.js 的核心 event-loop
- 开箱即用!Node.js+Mysql模块封装
- 前端号外—2022年最受欢迎居然是它,Node.js危已
- 如何使用jscythe并通过Node.js的Inspector机制执行任意JS代码
- JS种offsetTop、scrollTop、clientTop等各个属性的介绍详解编程语言
- js阻止点击事件的冒泡的实现详解编程语言
- JS this和调用对象
- Linux环境下配置Node.js(linux配置node)
- 快速在Linux平台上安装Node.js(node安装linux)
- 数据Node.js实现MySQL数据的同步更新(node同步mysql)
- Node.js与MongoDB:联手打造高效应用(node与mongodb)
- express使用Node.js、MongoDB和Express快速构建Web应用(nodemongodb)
- 利用Node.js实现对MS SQL服务器的连接(node连MSsql)
- IE和FireFox(FF)中js和css的不同
- 使用原生js封装webapp滑动效果(惯性滑动、滑动回弹)
- node.js中的path.basename方法使用说明
- node.js中的url.parse方法使用说明
- node.js中的fs.fstatSync方法使用说明
- node.js解决获取图片真实文件类型的问题
- js中的事件捕捉模型与冒泡模型实例分析