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 ...
随机推荐
- Django 查询集的过滤内置条件
条件选取querySet的时候,filter表示=,exclude表示!=.querySet.distinct() 去重复__exact 精确等于 like 'aaa' __iexact 精确等于 忽 ...
- Eclipse_java项目中导入外部jar文件
非常多时候,在java项目中须要导入外部 .jar文件.比方:须要导入数据库连接驱动等等一些包.不熟悉eclipse的人可能会犯愁,事实上非常easy. ...过程例如以下: 在须要加入外部文件的项 ...
- Linux CentOS6系统安装最新版本Node.js环境及相关文件配置
Node.js,当前应用非常广泛的Javascript运行环境,采用C++编写的,目前应用较多的用于WEB应用中,执行效率还是非常高的,虽然老左不从业程序的开发,但是有些时候在玩VPS的时候还是会遇到 ...
- JavaScript小游戏--2048(程序流程图)
- Linux入门基础介绍
概述: 1. linux是一个开源.免费的操作系统,其稳定性.安全性.处理多并发已经得到业界的认可,目前很多企业级的项目 都会部署到Linux/unix系统上. 2. 常见的操作系统(w ...
- 第五章.MyBatis高级映射
5.1 新建数据库准备 CREATE TABLE `finacial_products` ( `product_id` ) NOT NULL AUTO_INCREMENT, `name` ) NO ...
- 记录一次Git问题及其解决方案
错误信息:fatal: refusing to merge unrelated histories 错误产生背景:我将原先测试的项目本地删除后提交,然后将新的项目按照git的提交步骤进行提交,在最后一 ...
- OpenMax的接口与实现
OpenMax IL层的接口定义由若干个头文件组成,这也是实现它需要实现的内容,它们的基本描述如下所示. OMX_Types.h:OpenMax Il的数据类型定义 OMX_Core.h:OpenMa ...
- 用HTML编写迪士尼乐园页面
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/html"><head lang="e ...
- Oracle 体系结构四 逻辑和物理存储结构之间的关系
Oracle数据库从物理存储中完全抽象出逻辑存储.逻辑数据存储采用“段”的形式.段的类型有很多种:典型的段是“表”.这些段以物理形式存储在数据文件中.通过表空间将逻辑存储从物理存储中抽象出来.逻辑结构 ...