node.js(API解读) - file system模块

来源:互联网 发布:比较复杂的sql语句 编辑:程序博客网 时间:2024/06/05 00:23
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.stat(path, [callback])、fs.lstat(path,[callback])、fs.fstat(fd, [callback]) 和fs.statSync(path)、fs.lstatSync(path)、fs.fstatSync(fd)
文件信息,返回一个state对象,三者的区别在于:
第一个是返回文件的信息,参数是路径
第二个是和第一个一样,当路径是文件链接时,返回这个链接文件的信息
第三个是传递参数为fd文件描述符

5、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 所谓符号链接
硬链接就是备份,软连接就是快捷方式

6、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:删除某一个文件链接

 7、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:读取目录,以数组形式返回改目录的文件名,'.'和'..'除外

8、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 doesnot exist.
'r+' - Open file for reading and writing. An exception occurs ifthe file does not exist.
'w' - Open file for writing. The file is created (if it does notexist) or truncated (if it exists).
'w+' - Open file for reading and writing. The file is created (ifit does not exist) or truncated (if it exists).
'a' - Open file for appending. The file is created if it does notexist.
'a+' - Open file for reading and appending. The file is created ifit does not exist.
如果我们要做截取等操作,需要可写的flag,回调函数返回fd

9、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更改符号链接的时间戳。

10、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的倍数,效率较高。
当然写完应该关闭它。

11、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文件等。

12、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等等。

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

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

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

16、fs.FSWatcher

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