[Node.js] Read a File in Node.js with fs.readFile and fs.readFileSync
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] Read a File in Node.js with fs.readFile and fs.readFileSync的更多相关文章
- [Node.js] Write or Append to a File in Node.js with fs.writeFile and fs.writeFileSync
In node.js, you can require fs, and then call fs.writeFile with the filename, and data to write to t ...
- node.js delete directory & file system
node.js delete directory & file system delete a not empty directory https://nodejs.org/api/fs.ht ...
- [Whole Web, Node.js, PM2] Configuring PM2 for Node applications
In this lesson, you will learn how to configure node apps using pm2 and a json config file. Let's sa ...
- node.js fs.open 和 fs.write 读取文件和改写文件
Node.js的文件系统的Api //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile( ...
- 《Node.js高级编程》之Node 核心API基础
Node 核心API基础 第三章 加载模块 第四章 应用缓冲区 第五章 事件发射器模式简化事件绑定 第六章 使用定时器制定函数执行计划 第三章 加载模块 本章提要 加载模块 创建模块 使用node_m ...
- 《Node入门》读书笔记——用Node.js开发一个小应用
Android APP的开发告一段落,一个稳定的.实现了基本功能的APP已经交付用户使用了!我和老板交流了下,接下来准备转战Node.js了,而且一部分前端的功能也要做进去!哈哈哈~~~接下来要朝一个 ...
- 深入浅出Node.js(一) - 初识Node.js
1.Node.js将Javascript解决不确定性所使用的事件驱动方式引入了进来,因为JS是一门事件驱动的语言,旨在能够对外界的事件作出响应; 2.Node.js中,所有的有关异步的操作,都在同步操 ...
- node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法
1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...
- Edge.js:让.NET和Node.js代码比翼齐飞
通过Edge.js项目,你可以在一个进程中同时运行Node.js和.NET代码.在本文中,我将会论述这个项目背后的动机,并描述Edge.js提供的基本机制.随后将探讨一些Edge.js应用场景,它在这 ...
随机推荐
- Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级
Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级 // Find components upward function findComponen ...
- 失误1: 把i放到循环体内部,i++失效
54 while($lines_num_of_whole_table>=1){ 55 my $i = 1; ...
- idea java 注释模板配置
在网上找了好久,好多的文章都有一个共同的病点就是“@param注释当有多个参数时候,全部放在了一行里面”,非常不友好. 以下是我整理好的,完全按照eclipse的注释风格. !!!先看最后实现的效果图 ...
- c:if标签--判断不为空和其他的值判断
用<c:if test=""></c:if>标签时 <c:if test="${sl.chc_status==1 }"> ...
- Java集合系列之HashMap
概要 第1部分 HashMap介绍 HashMap简介 HashMap 是一个散列表,它存储的内容是键值对(key-value)映射.HashMap 继承于AbstractMap,实现了Map.Clo ...
- jquery点击tr换背景颜色
jquery点击tr换tr的背景颜色,table的id为db-list1jQuery(function() { jQuery("#db-list1 tr").click( func ...
- python 一些函数和类用法记录
这一篇主要用来记录在学习过程中遇到的一些觉得有意思的函数或者类的用法,有一些用法感觉很炫酷. 1.collections.defaultdict from collections import def ...
- git命令使用(二)
上次写的git命令,基本上能够支持一个项目的基本运行了,但是git不是就那几个命令还有一些其他的命令,来看一下 创建一个文件夹,想在这个文件夹下创建项目,就执行这个命令就行 $ git init 里面 ...
- CF666E Forensic Examination SAM+倍增,线段树和并
题面: 给你一个串S以及一个字符串数组T[1..m],q次询问,每次问S的子串S[p_l..p_r]在T[l..r]中的哪个串里的出现次数最多,并输出出现次数.如有多解输出最靠前的那一个. 分析: 第 ...
- Openjudge-4115-佐助和鸣人
这一题是一道广搜的题目,首先我们通过读入字符串读入每一行,然后顺带找到鸣人的位置. 然后我们初始化之后,就进行广搜,还是广搜的格式,但是要压入队列的条件我们可以稍微变一变,我们可以直接判断下一个要走的 ...