简单的nodejs 文件系统(fs)读写
2015-10-10 09:58
633 查看
在nodejs中,可以通过fs(file system)模块进行文件的I/O操作。
API链接地址:
http://nodeapi.ucdok.com/#/api/fs. href="http://www.2cto.com/kf/qianduan/css/" target=_blank>html
下面进行fs文件系统的使用实例:
1、模块调用声明:
var fs= require('fs');
var path = require('path');
fs为文件模块,path为系统路径模块。
2、可以使用writeFile方法,将数据写入文件到某个文件夹下。
fs.writeFile(filename, data, [options], callback)
filename为具体的文件保存路径地址,
data为具体要写入文件的数据对象,
[options]为具体的保存文件配置,编码格式等,
callback为具体的回调函数,进行相应的错误捕捉及提示。
代码如下:
fs.writeFile(path.join(__dirname, 'account.js'), JSON.stringify(tempAccount), function (err) {
if (err) throw err;
console.log("Export Account Success!");
});
以JSON格式将数据写入到文件路径下。
3、使用readFile方法,进行文件数据的读取。
fs.readFile(filename, [options], callback)
filename为文件路径及名称,
[options]为具体选项配置,包括数据的编码方式,
callback为回调函数,进行相应的错误处理及提示。
代码如下:
fs.readFile(path.join(__dirname, 'account.js'), function (err,bytesRead) {
if (err) throw err;
console.log(bytesRead);
});
读出数据二进制的流文件,如果需要为具体的数据,需要进行编码的配置,代码如下:
复制代码
fs.readFile(path.join(__dirname, 'account.js'),{encoding:'utf-8'}, function (err,bytesRead) {
if (err) throw err;
var data=JSON.parse(bytesRead);
console.log(data[0]);
console.log("readFile success");
});
4、读取文件夹下的相关ingwenj名称。
readdir(path,callback)
path为具体读取的文件夹路径地址,
callback为回调函数。
readdirSync(path)为读取文件的实时同步版本方法。
path为具体的文件夹路径地址。
var data=fs.readdirSync(__dirname);
console.log(data);
如此就实现了简单的文件写入及读取的实例,具体深入的应用还需进一步的学习。
API链接地址:
http://nodeapi.ucdok.com/#/api/fs. href="http://www.2cto.com/kf/qianduan/css/" target=_blank>html
下面进行fs文件系统的使用实例:
1、模块调用声明:
var fs= require('fs');
var path = require('path');
fs为文件模块,path为系统路径模块。
2、可以使用writeFile方法,将数据写入文件到某个文件夹下。
fs.writeFile(filename, data, [options], callback)
filename为具体的文件保存路径地址,
data为具体要写入文件的数据对象,
[options]为具体的保存文件配置,编码格式等,
callback为具体的回调函数,进行相应的错误捕捉及提示。
代码如下:
fs.writeFile(path.join(__dirname, 'account.js'), JSON.stringify(tempAccount), function (err) {
if (err) throw err;
console.log("Export Account Success!");
});
以JSON格式将数据写入到文件路径下。
3、使用readFile方法,进行文件数据的读取。
fs.readFile(filename, [options], callback)
filename为文件路径及名称,
[options]为具体选项配置,包括数据的编码方式,
callback为回调函数,进行相应的错误处理及提示。
代码如下:
fs.readFile(path.join(__dirname, 'account.js'), function (err,bytesRead) {
if (err) throw err;
console.log(bytesRead);
});
读出数据二进制的流文件,如果需要为具体的数据,需要进行编码的配置,代码如下:
复制代码
fs.readFile(path.join(__dirname, 'account.js'),{encoding:'utf-8'}, function (err,bytesRead) {
if (err) throw err;
var data=JSON.parse(bytesRead);
console.log(data[0]);
console.log("readFile success");
});
4、读取文件夹下的相关ingwenj名称。
readdir(path,callback)
path为具体读取的文件夹路径地址,
callback为回调函数。
readdirSync(path)为读取文件的实时同步版本方法。
path为具体的文件夹路径地址。
var data=fs.readdirSync(__dirname);
console.log(data);
如此就实现了简单的文件写入及读取的实例,具体深入的应用还需进一步的学习。
相关文章推荐
- 让nodejs在iis上运行
- node-formidable详解
- nodejs-post文件上传原理详解
- LeetCode #19 Remove Nth Node From End of List (E)
- 一些node.js的一些资源
- node.js定时任务:node-schedule的使用
- NODE.JS玩玩
- Node.js 本地版本控制
- javascript, jquery, nodejs学习2
- nodejs Error: request entity too large解决方案
- LINK : warning LNK4098: 默认库“MSVCRTD”与其他库的使用冲突;请使用 /NODEFAULTLIB:library
- Node.js cannot find module ***
- node.js发送邮件email
- node lesson4--eventproxy不懂
- 深入浅出Node.js读书笔记
- nodejs 上传图片(服务端输出全部代码)
- ZenNodeDiscovery
- 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0
- node.js下LDAP查询实践
- 使用nodejs+express(4.x+)实现文件上传