用node.js读写文件
node.js没有二进制数据类型,却提供了类似字节数组的“流“数据类型,着一种数据类型在文件系统模块中频频出现
- node.js打开文件
fs = require('fs');
console.log('准备打开文件');
fs.open('/etc/hosts','r+',function (err,fd) {
if (err)
{
console.log('damn~打开错误');
}
console.log('可以打开');
fs.close(fd,function (err) {
if (err)
{
console.error(err)
}
console.log('顺利关闭')
});
});
- 把文件内容读入缓冲区,并把缓冲区内容解读为utf8模式,(16进制也可以哦)
fs = require('fs');
fs.open('/etc/hosts','r+',function (err,fd) {
var mybuffer = Buffer.alloc(1024);
offset=0;
len = mybuffer.length;
fileposition = null;
fs.read(fd,mybuffer,offset,len,fileposition,function(err,readByte){
console.log("可读取数据数量"+readByte);
var wuwa=mybuffer.slice(0,readByte);
console.log("缓冲区内容解读前:",wuwa);
console.log("缓冲区内容解读后:",wuwa.toString('utf8'));
});
});
输出结果:
可读取数据数量196
缓冲区内容解读前: <Buffer 2e 2e 2e 6c 6f 6c 6f 6c 6f 6c 6f 2e 6c 6f 6c 6f 6d 6e 6c 6f 6c ... more bytes>
缓冲区内容解读后: 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 coldspring.net taizhouwu.net mydb.net
:: localhost localhost.localdomain localhost6 localhost6.localdomain6
- 异步读取(data仍为缓冲区)
fs = require('fs');
fs.readFile('/etc/hosts',function (err,data) {
if(err)
{
console.error(err);
}
console.log(data.toString('utf8'));#仍旧是buffer类型,需要转换为utf8类型
});
输出结果:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
:: localhost localhost.localdomain localhost6 localhost6.localdomain6
用node.js读写文件的更多相关文章
- node.js读写文件
关于node.js的读写操作,应用场景有很多.比如其中这样的一个场景,如何获取全局的token.这就涉及到写和读操作了. 写操作: var fs = require("fs"); ...
- Node.js写文件的三种方法
Node.js写文件的三种方式: 1.通过管道流写文件 采用管道传输二进制流,可以实现自动管理流,可写流不必当心可读流流的过快而崩溃,适合大小文件传输(推荐) var readStream = fs. ...
- Node.js读取文件内容
原文链接:http://blog.csdn.net/zk437092645/article/details/9231787 Node.js读取文件内容包括同步和异步两种方式. 1.同步读取,调用的是r ...
- node.js grunt文件压缩
对于前段来说,熟悉node的人其实还并不是太多,如果您想入门一门后端语言我建议还是从node入手最好. 我也是最近开始学习node,来谈谈近期对node的学习的心得. 提到node首先就是要安装一大堆 ...
- electron node.js 实现文件拖动读取文件
css/styles.css .for_file_drop { width: 100%; height: 100px; background-color: blueviolet; } index.ht ...
- 又拍云 Node.js 实现文件上传、删除
Node.js 服务端 使用 Node.js + Express.js 实现 服务端 const express = require("express"); const app = ...
- js 读写文件
读写文件: var f = fso.CreateTextFile("c:\\pexam\\"+name+".txt", true); f.write(arr); ...
- node.js 从文件流中读写数据及管道流
读取数据 // 引入 fs 模块 const fs = require('fs'); // 创建可读流 let readStream = fs.createReadStream('index.txt' ...
- node.js之文件读写模块,配合递归函数遍历文件夹和其中的文件
fs.stat会返回文件夹会文件的属性 var fs = require('fs'); var wenwa = function (pathname,callback) { fs.stat(pathn ...
随机推荐
- The timeout period elapsed prior to completion of the operation or the server is not responding.
问题:更新数据的状态值时,部分报出如下异常: 即时有成功更新,时有报错问题出现. 在LOG中发现成功更新的数据,存在更新时间过长问题,将近30秒(EF默认的CommandTimeout为30秒): 代 ...
- @RequestBody以及@RequestParam的使用过程区别
查考地址:https://blog.csdn.net/justry_deng/article/details/80972817 待整理中.....
- SpringBoot导入mail依赖报错
报错:Missing artifact org.springframework.boot:spring-boot-starter-mail:jar:2.0.3 之前导入log4j时报的一样的错误,最后 ...
- 高深的dp POJ 2229Sumsets
对于这个问题, 我们显然可以看出来, 当他是奇数的时候, 直接等于他的前一个偶数 dp [ i ] = dp [ i - 1] ; 那么问题, 当它是偶数的时候, 我们应该怎么进行 dp 记忆化搜索并 ...
- 【AC自动机】最短母串
[题目链接] https://loj.ac/problem/10061 [题意] 给定 n 个字符串 S1-Sn,要求找到一个最短的字符串 T,使得这 n 个字符串都是 T 的子串. [题解] 类似于 ...
- 用Lua的协程实现类似Unity协程的语句块
local co_time_tbl = {} setmetatable(co_time_tbl, { __len = function(o) for k, v in pairs(o) do count ...
- 原生 JS 绑定事件 移除事件
监听事件的绑定与移除主要是addEventListener和removeEventListener的运用. addEventListener语法 element.addEventListener(ty ...
- SSM框架警告/错误集合
警告: 1.使用Eclipse的Spring Elements组件的时候发现会提示有警告:Expect at least one bean match() 解决办法:项目可以正常运行,未有报错,在其他 ...
- 【小知识点】去除inline-block元素间间距的办法
之前一直用float浮动方法布局,因为display:inline-block有间隙,现在找到办法了.在父元素上面加font-sise:0,就可以了. 效果如图: 代码如下: <!DOCTYPE ...
- 三 HashSet
HashSet无序且不能重复 1.HashSet类的字段属性 //HashSet集合中的内容是通过 HashMap 数据结构来存储的 private transient HashMap<E,Ob ...