文件模块fs
Buffer(缓冲区)
它表示在 V8 JavaScript 引擎外部分配的固定大小的内存块
和数组很像;数组不能用来存储二进制的文件,Buffer就是专门用来存储二进制文件的
使用Buffer不需要引入模块,直接使用即可
在buffer中存储的都是二进制数据,但是在显示时,都是以16进制显示,2进制太长
Buffer的大小一旦确定,则不能修改,它实际上是对底层内存的直接操作
buffer中的一个元素,占用内存的一个字节
8bit =1byte (字节)
1024byte = 1kb
1024kb =1mb
1024mb =1gb
1024gb =1tb
一个英文字母占用一个字节,一个中文占用3个字节
基本语法
let buf=Buffer.from(str) // 将字符串转换为buffer
buf.length // buf占用多少个字节
Buffer.alloc(size) // 创建一个指定大小的Buffer
Buffer.allocUnsafe(size) // 创建一个指定大小的Buffer,但可能存在敏感数据,分配一个连续空间,但是不会清除原有数据
buf.toString() // 将buffer转为字符串
文件的写入
服务器的本质就是将本地的文件发送给远程客户端;fs模块中的所有操作都有两种形式可供选择:同步和异步
同步文件系统会阻塞程序的执行,异步不会,它在操作完成时,通过回调函数将结果返回
同步文件的写入
1》打开文件
fs.openSync(path,flags [, mode] )
path:要打开的文件路径
flags:打开文件需要操作的类型
模式 | 说明 | |
---|---|---|
r | 读取文件,文件不存在则出现异常 | |
w | 打开文件用于写入操作(会覆盖),如果文件不存在会创建,存在则会覆盖内容 | |
a | 打开文件用于追加内容,如果没有文件则创建 |
mode: 设置文件的操作权限,一般不传
2》像文件中写入内容
会返回一个文件描述符如作为结果,我们可以通过文件描述符对文件进行各种操作
const fd= fs.writeSync(fd,string [,position, endoding])
fd:文件的描述符
string: 要写入的内容
positioin: 写入的起始位置
encoding: 写入的编码,默认是utf-8
3》保存并关闭文件
fs.closeSync(fd)
fs是核心模块,不需要下载,直接引入使用
var fs = require("fs");
var fd = fs.openSync("hello.txt", "w");
fs.writeSync(fd, "爱你");
fs.closeSync(fd);
异步文件的写入
基本步骤:打开–写入–关闭
fs.open(path,flags [,mode ], callback)
回调函数有两个参数:err:错误对象,没错误则为null , fd 文件描述符
fs.write(fd,string [,position, endoding] , callback)
fs.close()
const fs = require("fs");
fs.open("hello2.txt", "w", function (err, fd) {
// console.log(arguments);
if (!err) {
fs.write(fd, "我也爱你-异步", function (err) {
if (err) {
console.log(err);
}
fs.close(fd, function (err) {
if (!err) {
console.log("文件关闭成功");
}
});
});
} else {
console.log(err);
}
});
简单文件写入
异步:
fs.writeFile(filePath,dataContent, [, options] , callback)
filePath:写的文件路径
dataContent:写入的内容、数据
options 可选项 { encoding:默认utf-8, mode:默认0o666, flag:默认w }
callback 回调函数
const fs = require("fs");
fs.writeFile("simple.txt", "简单异步文件的操作", function (err) {
if (!err) {
console.log("写入成功");
}
});
同步
fs.writeFileSync(filePath,dataContent, [, options] )
流式文件的写入
创建一个流式文件
fs.createWriteStream(path [, options])
path: 操作文件路径
简单的流写入:
const fs = require("fs");
// 创建流
const ws = fs.createWriteStream("stream.txt");
// 监听流的打开与关闭,只打开关闭一次,所以用的once
ws.once("open", function () {
console.log("流打开了");
});
ws.once("close", function () {
console.log("流关闭了");
});
// 写入内容,通过ws输入
ws.write("锄禾日当午;");
ws.write("学习靠自主;");
ws.write("工作真辛苦;");
ws.write("像一个保姆;");
// 关闭流
ws.end();
文件的读取
简单文件的读取
异步:
fs.readFile(path [,options], callback)
返回的是buffer
const fs = require("fs");
fs.readFile("./hello.txt", function (err, data) {
if (!err) {
// 返回的是buffer类型的数据,因为读取的也不一定是字符串,所以不能直接返回字符串
console.log(data);
console.log(data.toString());
}
});
同步:
const data=fs.readFileSync(path [,options])
流式文件的读取
流式文件读取也适用于一些比较大的文件,可以分多次将文件读取到内存中
fs.createReadStream(path[, options])
读取可读流中的数据,需要为可读流绑定一个data事件,绑定完毕他会自动开始读取数据,读取完之后会自动关闭可读流
读取到的是buffer数据
const fs = require("fs");
const rs = fs.createReadStream("./stream.txt");
rs.once("open", function () {
console.log("可读流打开");
});
rs.once("close", function () {
console.log("可读流关闭");
});
rs.on("data", function (data) {
console.log(data); // buffer
});
将读取到的流放到另一个文件中
const fs = require("fs");
const rs = fs.createReadStream("./stream.txt");
const ws = fs.createWriteStream("./copyStream.txt");
rs.once("close", function () {
console.log("可读流关闭");
ws.end();
});
rs.on("data", function (data) {
ws.write(data);
});
pipe可以将可读流中的内容直接输出到可写流
const fs = require("fs");
const rs = fs.createReadStream("./bgc.jpg");
const ws = fs.createWriteStream("./copyBgc.jpg");
rs.pipe(ws);
其他操作
验证路径是否存在
const isExist=fs.existsSync(path)
获取文件信息
fs.stat(path,callback)
fs.statSync(path)
const fs = require("fs");
fs.stat("./hello.txt", function (err, Stats) {
if (!err) {
console.log(Stats);
}
});
删除文件
fs.unlink(path,callback)
fs.unlinkSync(path)
列出文件
返回一个字符串数组
fs.readdir(path [, options] ,callback)
fs.readdirSync(path [,options])
截断文件
截断文件,将文件修改为指定大小(字节)
fs.truncate(path,len,callback)
fs.truncateSync(path,len)
fs.truncateSync(“./hello2.txt”, 100)
没有这么多字节他会拼上null;如果一个中文字没有截全,截到一半就会认不出来,就是显示菱形问好
建立目录
fs.mddir(path [,mode],callback)
fs.mddirSync(path [,mode])
删除目录
fs.rmdir(path,callback)
fs.rmdirSync(path)
重命名文件和目录
fs.rename(oldPath,newPath,callback)
fs.renameSync(oldPath,newPath)
监视我呢见更改写入
fs.watchFile(filename [,options] , lintenerFunc)
options中有个属性interval : 多久更新一次,默认值5007
fs.watchFile("./hello.txt", function (cur, pre) {
console.log(cur.size, pre.size);
});
更多阅读官方文档:http://nodejs.cn/api/