nodejs 文件系统
fs.open(path,flags,[mode],callback)
fs.openSync(path,flags,[mode])
fs.close(fd,callback)
fs.closeSync(fd)
fs.open('myFile','w',function(err,fd){
if(!error){
fs.close(fd);
}
}
注意:回调函数被指定,并接收err和fd参数,fd参数是你可以用来读取或写入该文件的文件描述符。
fs.writeFile(path,data,[options],callback)
fs.writeFileSync(path,data,[options])
var fs=require('fs');
var config={
maxFiles:20,
maxConnections:15,
rootPath:'./routes'
};
var configTxt=JSON.stringify(config);
var options={
encoding:'utf8',
flag:'w'
};
fs.writeFile('config.text',configTxt,options,function(err){
if(err){
console.log('config write fail');
} else{
console.log('config write success');
}
});


(2)同步和异步文件写入
fs.writeSync(fd,data,offset,length,position)
fs.write(fd,data,offset,length,position,callback)
var fs=require('fs');
var fruitBow1=['apple','orange','banana'];
function writeFruit(fd){
if(fruitBow1.length){
var fruit=fruitBow1.pop()+'';
fs.write(fd,fruit,null,null,function(err,bytes){
if(err){
console.log('File write failed.');
}else{
console.log('write: %s %dbytes',fruit,bytes);
writeFruit(fd); //反复调用,直到fruitBow1的length为0
}
});
}else{
fs.close(fd);
}
}
fs.open('fruit.txt','w',function(err,fd){
writeFruit(fd);
});

fs.createWriteStream(path,[options])
var fs=require('fs');
var grains=['wheat','rice','oats'];
var options={encoding:'utf8',flag:'w'};
var fileWriteStream=fs.createWriteStream('grains.txt',options);
fileWriteStream.on('close',function(){
console.log('file closed.');
});
while(grains.length){
var data=grains.pop()+' ';
fileWriteStream.write(data);
console.log('write: %s',data);
}
fileWriteStream.end();

fs.readFile(path,[options],callback)
fs.readFileSync(path,[options])
var fs=require('fs');
var options={encoding:'utf8',flag:'r'};
fs.readFile('config.txt',options,function(err,data){
if(err){
console.log('Failes to open config File.');
}else{
console.log('config Loaded.');
var config=JSON.parse(data);
console.log('Max Files: '+config.maxFiles);
console.log('maxConnections: '+config.maxConnections);
console.log('rootPath: '+config.rootPath);
}
});


fs.readSync(fd,buffer,offset,length,position)
fs.read(d,buffer,offset,length,position,callback)
var fs=require('fs');
var options={encoding:'utf8',flag:'r'};
var fileReadStream=fs.createReadStream('grains.txt',options);
fileReadStream.on('data',function(chunk){
console.log('Grains: %s',chunk);
console.log('read %d bytes of data.',chunk.length);
});
fileReadStream.on('close',function(){
console.log('file closed.');
});


nodejs 文件系统的更多相关文章
- 简单的nodejs 文件系统(fs)读写例子。
在nodejs中,可以通过fs(file system)模块进行文件的I/O操作. API链接地址: http://nodeapi.ucdok.com/#/api/fs.html 下面进行fs文件系统 ...
- nodeJs文件系统(fs)与流(stream)
一.简介 本文将介绍node.js文件系统(fs)和流(stream)的一些API已经参数使用情况. 二.目录 文件系统将介绍以下方法: 1.fs.readFile 2.fs.writeFile 3. ...
- nodejs(二) --- 重要知识点回顾
1. 运行一个nodejs文件, 如一个js文件中只含有console.log("hello world");的文件,我们再git里运行node,即 node hello.js 即 ...
- 文件系统(node.js学习笔记)
根据nodejs菜鸟教程整理. 官方API文档:nodeJS文件系统API 其他整理:nodejs File System 文件系统操作函数分类 1.引用: 导入文件系统模块(fs)语句:var fs ...
- node-webkit 使用requirejs 小结
1. node-webkit 启动页使用requrejs 将webapp中的require改为requirejs,因为requirejs本来就应该用requirejs的,require只是requir ...
- EJS 是什么 ,怎么用,以及优点
一.什么是EJS EJS是一个JavaScript模板库,用来从JSON数据中生成HTML字符串. 二.为什么要使用EJS 与最初的JavaScript相比较,一些不太了解你的代码的人可以更容易地通过 ...
- nodeJS之fs文件系统
前面的话 fs文件系统用于对系统文件及目录进行读写操作,本文将详细介绍js文件系统 概述 文件 I/O 是由简单封装的标准 POSIX 函数提供的. 通过 require('fs') 使用该模块. 所 ...
- 在nodeJS中操作文件系统(二)
在nodeJS中操作文件系统(二) 1. 移动文件或目录 在fs模块中,可以使用rename方法移动文件或目录,使用方法如下: fs.rename(oldPath,newPath,call ...
- NodeJS学习笔记 (2)文件系统操作-fs(ok)
原文:https://github.com/chyingp/nodejs-learning-guide/blob/master/%E6%A8%A1%E5%9D%97/fs.md#%E9%80%9A%E ...
随机推荐
- 51nod 1019 逆序数(逆序数+离散化)
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数. 如2 4 3 1中,2 1,4 3,4 1,3 1是 ...
- GCD学习(五) dispatch_barrier_async
先看段代码 dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISP ...
- UVa 12034 - Race(递推 + 杨辉三角)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- gluoncv 目标检测,训练自己的数据集
https://gluon-cv.mxnet.io/build/examples_datasets/detection_custom.html 官方提供两种方案,一种是lst文件,一种是xml文件(v ...
- ZOJ Monthly, January 2019 Little Sub and his Geometry Problem 【推导 + 双指针】
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5861 Little Sub and his Geometry Prob ...
- Jmeter实例计划(查询天气)
查询计划(天气查询) 这是一个入门计划,仅使用jmeter进行查询功能的计划,可参考http://www.cnblogs.com/TankXiao/p/4045439.html有代码可下载.不过我还是 ...
- 使用QT开发GoogleMap瓦片显示和下载工具
之前做项目的时候经常遇到需要大量地图背景数据,然后没有数据被逼着去Google上下载瓦片数据在拼接成整张影像的工作,其实遥感影像晚上有很多可以下载到的,但是大部分是作为研究用的,作为GIS的背景图或者 ...
- 在EF Core里面如何使用以前EntityFramework的DbContext.Database.SqlQuery<SomeModel>自定义查询
问: With Entity Framework Core removing dbData.Database.SqlQuery<SomeModel> I can't find a solu ...
- 撸一个简单的MVVM例子
我个人以为mvvm框架里面最重要的一点就是VM这部分,它要与Model层建立联系,将Model层转换成可以被View层识别的数据结构:其次也要同View建立联系,将数据及时更新到View层上,并且响应 ...
- SpringSecurity
1.1 SpringSecurity技术简介与使用 1.1.1 简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架. ...