一、打开和关闭文件 //在不同的操作系统中表现有所不同
- var fd=fs.open(path,flags,[mode],callback) //返回一个整型 表示打开的文件对象
- fs.close(fd,callback)
附:flags参数表,表示打开文件的模式
- ‘r‘ - Open file for reading. An exception occurs if the file does not exist.
‘r+‘
- Open file for reading and writing. An exception occurs if the file does not exist.‘rs+‘
- Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.This is primarily useful for opening files on NFS mounts as it allows you to skip the potentially stale local cache. It has a very real impact on I/O performance so don‘t use this flag unless you need it.
Note that this doesn‘t turn
fs.open()
into a synchronous blocking call. If that‘s what you want then you should be usingfs.openSync()
‘w‘
- Open file for writing. The file is created (if it does not exist) or truncated (if it exists).‘wx‘
- Like‘w‘
but fails ifpath
exists.‘w+‘
- Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).‘wx+‘
- Like‘w+‘
but fails ifpath
exists.‘a‘
- Open file for appending. The file is created if it does not exist.‘ax‘
- Like‘a‘
but fails ifpath
exists.‘a+‘
- Open file for reading and appending. The file is created if it does not exist.‘ax+‘
- Like‘a+‘
but fails ifpath
exists.
二、写入文件
1、简单文件写入
fs.writeFile(fileName,data,[opations],callback)
- fineName:指定文件
- data:指定将被写入到文件的String或Buffer对象
- opations:是一个对象,可以包含定义字符串编码,以及打开文件时使用的模式和标志的encoding、mode和、flag属性
- callback:当文件写入已经完成时被调用的函数
2、同步文件写入
var fd=fs.opneSync(path,flags[,mode])
fs.writeSync(fd,buffer,offset,length[,position])
fs.writeSync(fd,data[,position][,encoding])
fs.closeSync(fd)
- path:指定文件
- flags:指定打开文件的模式
- mode:设置文件的访问模式
- fd:整数,代表文件对象,一般是fs.open()函数的返回值
- data:写入的数据
- buffer:缓冲数据
- offset/length
:
确定从哪个位置开始写入buffer。
3、异步写入文件
- fs.open(path, flags[, mode], callback) //借助callback(err,fd)函数实现异步写入文件
- fs.write(fd, buffer, offset, length[, position], callback)
- fs.write(fd, data[, position[, encoding]], callback)
- fs.close(fd)
4、流式文件写入
- var fileWriteStream=fs.createWriteStream(path[,options])
- fileWriteStream.write(data)
- fileWriteStream.on("close",handler) //end方法执行后调用
- fileWriteStream.end()
三、读取文件(同上)
1、简单文件读取
- fs.readFile(file[, options], callback)
2、同步文件读取
- fs.readSync(fd, buffer, offset, length, position)
3、异步文件读取
- fs.read(fd, buffer, offset, length, position, callback)
4、流式文件读取
- fs.createReadStream(path[, options])
- data事件和close事件
四、其他文件系统任务
1、fs.stat(path, callback) //获取文件信息
- stats.isFile() //如果该条目是一个文件,返回true
stats.isDirectory() //如果条目是一个目录,返回true
stats.isBlockDevice() //
stats.isCharacterDevice() //
stats.isSymbolicLink()
(only valid with fs.lstat()) //stats.isFIFO() //
stats.isSocket() 如果条目是一个套接字,返回true
2、列出文件(列出path路径的所有子目录,以数组方式存储)
- fs.readdir(path[, options], callback)
3、删除文件
- fs.unlink(path,callback)
4、截断文件
- fs.truncate(path, len, callback) //从文件开始出截取len长度,将其后舍弃
5、建立和删除目录
- fs.mkdir(path[, mode], callback) //建立
- fs.rmdir(path, callback) //删除
6、重命名文件和目录
- fs.rename(oldPath, newPath, callback)
7、监视文件更改入
- fs.watchFile(filename[, options], listener) //options包含peristent(是否持续)和interval(间隔检查)属性