Node.Js API 解析--FS模块

文件系统

1、fs.rename(path1, path2, [callback]) 和 fs.renameSync(path1, path2)

运行代码如下,就可以将test2.js改名为test3.js
var fs = require('fs')
fs.rename('./test2.js', './test3.js', function (err) {
  if (err) throw err;
  console.log('renamed complete');
});


2、fs.truncate(fd, len, [callback]) 和 fs.truncateSync(fd, len)

截断某个文件,我们试一下:
var fs = require('fs');
fs.open(
'./ex.js', function(err, fd){
fs.truncate('./ex.js', 10, function (err) {
    if (err) throw err;
        console.log('fd');
    });

})


3、fs.chmod(path, mode, [callback]) 和 fs.chmodSync(path, mode)

修改文件权限,第二个参数mode是一个三位数,777或666。实际上是:-rwxrwxrwx,三个一组,r:4、w:2、x:1。

4、fs.chown

修改文件所有者

5、fs.stat(path, [callback])、fs.lstat(path, [callback])、fs.fstat(fd, [callback]) 和 fs.statSync(path)、fs.lstatSync(path)、fs.fstatSync(fd)
文件信息,返回一个state对象,三者的区别在于:
第一个是返回文件的信息,参数是路径
第二个是和第一个一样,当路径是文件链接时,返回这个链接文件的信息
第三个是传递参数为fd文件描述符

6、fs.link(srcpath, dstpath, [callback])、fs.linkSync(srcpath, dstpath) 和 fs.symlink(linkdata, path, [callback])、fs.symlinkSync(linkdata, path)
建立文件链接,link和symlink的区别是:
link 创建的是hard link 所谓硬链接
symlink创建的是symbolic link 所谓符号链接
硬链接就是备份,软连接就是快捷方式

7、fs.readlink(path, [callback])、fs.realpath(path, [callback]) 、fs.unlink(path, [callback])和 fs.readlinkSync(path)、fs.realpathSync(path)、fs.unlinkSync(path)
这3个函数分别是:
1、readlink:读取链接源地址
2、realpath:根据相对地址转换为绝对地址
3、unlink:删除某一个文件链接

8、fs.rmdir(path, [callback])、fs.mkdir(path, mode, [callback])、fs.readdir(path, [callback]) 和 fs.rmdirSync(path)、fs.mkdirSync(path, mode)、fs.readdirSync(path)
三个命令分别是:
rmdir:删除目录
mkdir:建立目录
readdir:读取目录,以数组形式返回该目录的文件名,'.'和'..'除外

9、fs.close(fd, [callback])、fs.closeSync(fd) 和 fs.open(path, flags, [mode], [callback])、fs.openSync(path, flags, [mode])
fs.close:关闭文件,参数为FD文件描述符,
fs.open:第二个参数是关键,见下表:
'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.
'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
'a' - Open file for appending. The file is created if it does not exist.
'a+' - Open file for reading and appending. The file is created if it does not exist.
如果我们要做截取等操作,需要可写的flag,回调函数返回fd

10、fs.utimes(path, atime, mtime, callback)、fs.utimesSync(path, atime, mtime) 和 fs.futimes(path, atime, mtime, callback)、
fs.futimesSync(path, atime, mtime)

更改文件时间戳,2者区别在于,utimes更改此文件时间戳,如果此文件指向一个符号链接,futimes更改符号链接的时间戳。

11、fs.write(fd, buffer, offset, length, position, [callback]) 和 fs.writeSync(fd, buffer, offset, length, position) 和 fs.writeSync(fd, str, position, encoding='utf8')
文件写入方法,第三为同步写入字符串。
这里是一个写文件的例子:
var fs = require('fs');
fs.open('./ex.js', 'w', function(err , fd){
    var buf = new Buffer(256);
    var len = buf.write('12321321321321', 0)
    fs.write(fd, buf, 0, len, 0, function(err, w){
        console.log(w)
    })
})

注意:buffer尺寸的大小设置最好是8的倍数,效率较高。
当然写完应该关闭它。

12、fs.read(fd, buffer, offset, length, position, [callback]) 、fs.readSync(fd, buffer, offset, length, position)、fs.readSync(fd, length, position, encoding) 和 fs.readFile(filename, [encoding], [callback])、fs.readFileSync(filename, [encoding])
前3个是读取文件描述符和BUFFER的方法,后2个是读取文件全部内容,比如输出html模版,或者css文件等。

13、fs.watchFile(filename, [options], listener)、 fs.writeFileSync(filename, data, encoding='utf8')、fs.unwatchFile(filename)、fs.watch(filename, [options], listener)
watchfile:
对文件的监听方法,第二个参数是可选项,如果指定了options参数,它应该是一个包含如下内容的对象:名为persistent的布尔值,和名为interval单位为毫秒的轮询时间间隔,默认值为{ persistent: true, interval: 0 }。
listener回调函数为有2个参数,分别是curr, prev,这个2个对象是stat的实例。
unwatchfile:取消监听文件
watch:监听文件或是文件夹,返回event, filename,event可以是rename或是change等等。

14、fs.Stats
这个是fs.stat()方法的回调返回值,他不仅包括上述的属性还包括以下方法:
stats.isFile()
stats.isDirectory()
stats.isBlockDevice()
stats.isCharacterDevice()
stats.isSymbolicLink() (only valid with fs.lstat())
stats.isFIFO()
stats.isSocket()

15、fs.ReadStream
创建一个文件可读流:
fs.createReadStream(path, [options])

16、fs.WriteStream
创建一个文件可写流:
fs.createWriteStream(path, [options])

17、fs.FSWatcher

watcher.close() :关闭监听
Event: 'change' :事件改变
Event: 'error' : 事件错误




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值