Node.js文件操作方法汇总

Node.js和其他语言一样,也有文件操作。先不说node.js中的文件操作,其他语言的文件操作一般也都是有打开、关闭、读、写、文件信息、新建删除目录、删除文件、检测文件路径等。在node.js中也是一样,也都是这些功能,可能就是api与其他语言不太一样。

一、同步、异步打开关闭

/** * Created by Administrator on 2016/3/21. */ var fs=require("fs"); //同步读 fs.openSync = function(path, flags, mode) //模块fs.js文件中如上面定义的openSync 函数3个参数 //.1.path 文件路径 //2.flags 打开文件的模式 //3.model 设置文件访问模式 //fd文件描述 var fd=fs.openSync("data/openClose.txt",'w'); //fs.closeSync = function(fd) fs.closeSync(fd); //异步读写 //fs.open = function(path, flags, mode, callback_) //fs.close = function(fd, callback) fs.open("data/openColse1.txt",'w',function(err,fd) { if (!err) { fs.close(fd,function(){ console.log("closed"); }); } });

其中的flags其他语言也会有.其实主要分3部分 r、w、a.和C中的差不多。

1.r —— 以只读方式打开文件,数据流的初始位置在文件开始
2.r+ —— 以可读写方式打开文件,数据流的初始位置在文件开始
3.w ——如果文件存在,则将文件长度清0,即该文件内容会丢失。如果不存在,则尝试创建它。数据流的初始位置在文件开始
4.w+ —— 以可读写方式打开文件,如果文件不存在,则尝试创建它,如果文件存在,则将文件长度清0,即该文件内容会丢失。数据流的初始位置在文件开始
5.a —— 以只写方式打开文件,如果文件不存在,则尝试创建它,数据流的初始位置在文件末尾,随后的每次写操作都会将数据追加到文件后面。
6.a+ ——以可读写方式打开文件,如果文件不存在,则尝试创建它,数据流的初始位置在文件末尾,随后的每次写操作都会将数据追加到文件后面。

二、读写

1.简单文件读写

/** * Created by Administrator on 2016/3/21. */ var fs = require('fs'); var config = { maxFiles: 20, maxConnections: 15, rootPath: "/webroot" }; var configTxt = JSON.stringify(config); var options = {encoding:'utf8', flag:'w'}; //options 定义字符串编码 打开文件使用的模式 标志的encoding、mode、flag属性 可选 //异步 //fs.writeFile = function(path, data, options, callback_) //同步 //fs.writeFileSync = function(path, data, options) fs.writeFile('data/config.txt', configTxt, options, function(err){ if (err){ console.log("Config Write Failed."); } else { console.log("Config Saved."); readFile(); } }); function readFile() { var fs = require('fs'); var options = {encoding:'utf8', flag:'r'}; //异步 //fs.readFile = function(path, options, callback_) //同步 //fs.readFileSync = function(path, options) fs.readFile('data/config.txt', options, function(err, data){ if (err){ console.log("Failed to open Config File."); } else { console.log("Config Loaded."); var config = JSON.parse(data); console.log("Max Files: " + config.maxFiles); console.log("Max Connections: " + config.maxConnections); console.log("Root Path: " + config.rootPath); } }); }

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe SimpleReadWrite.js Config Saved. Config Loaded. Max Files: 20 Max Connections: 15 Root Path: /webroot Process finished with exit code 0

2.同步读写

/** * Created by Administrator on 2016/3/21. */ var fs = require('fs'); var veggieTray = ['carrots', 'celery', 'olives']; fd = fs.openSync('data/veggie.txt', 'w'); while (veggieTray.length){ veggie = veggieTray.pop() + " "; //系统api //fd 文件描述 第二个参数是被写入的String或Buffer // offset是第二个参数开始读的索引 null是表示当前索引 //length 写入的字节数 null一直写到数据缓冲区末尾 //position 指定在文件中开始写入的位置 null 文件当前位置 // fs.writeSync(fd, buffer, offset, length[, position]); // fs.writeSync(fd, string[, position[, encoding]]); //fs.writeSync = function(fd, buffer, offset, length, position) var bytes = fs.writeSync(fd, veggie, null, null); console.log("Wrote %s %dbytes", veggie, bytes); } fs.closeSync(fd); var fs = require('fs'); fd = fs.openSync('data/veggie.txt', 'r'); var veggies = ""; do { var buf = new Buffer(5); buf.fill(); //fs.readSync = function(fd, buffer, offset, length, position) var bytes = fs.readSync(fd, buf, null, 5); console.log("read %dbytes", bytes); veggies += buf.toString(); } while (bytes > 0); fs.closeSync(fd); console.log("Veggies: " + veggies);

"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe syncReadWrite.js Wrote olives 7bytes Wrote celery 7bytes Wrote carrots 8bytes read 5bytes read 5bytes read 5bytes read 5bytes read 2bytes read 0bytes Veggies: olives celery carrots Process finished with exit code 0

3.异步读写 和同步读写的参数差不多就是多了callback

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgyjwj.html