fs.rename() fs.renameSync
fd = fs.openSync(file, flags) fs.closeSync(fd) // fs.open(file, flags, function (fd) { fs.close(fd, function(err) { }) })
当打开或新建文件时,内核向进程返回一个文件描述符
fd`flags
表示打开模式: 'r'以读取模式打开,'w' 以读取模式打开,不存在则建立,'a' 以追加模式打开,不存在则建立var fd = fs.openSync(file, frag) fs.ftruncateSync(fd, len) fs.closeSync(fd); // var fd = fs.openSync(file, frag) fs.ftruncate(fd, len, function (err) { fs.closeSync(fd); })
fs.truncateSync(file, len) // fs.truncate(file, len, function (err) { })
fs.ftruncate
相似,但能够直接经过路径修改文件stat\statSync
var fs = require('fs') fs.stat(path, function (err, stats) { console.log('是否为文件:', stats.isFile()); console.log('是否为目录:', stats.isDirectory()); console.log('读写权限是:', stats.mode); console.log('文件大小是:', stats.size); console.log('访问时间是:', stats.atime); console.log('修改时间是:', stats.mtime); console.log('建立时间是:', stats.ctime); console.log('全部权用户ID :', stats.uid); console.log('全部权群组ID:', stats.gid); })
fstat\fstatSync
fs.chown(path, uid, gid, callback)/chownSync
fs.fchown(fd, uid, gid, callback)/fchowmSync
fs.chmod(path, mod, callback)/chmodSync
fs.fchmods(fd, mod, callback)/fchmodSync
chmod mod path
; 查看相应文件权限: ls -lh path
Linux/Unix 的档案存取权限分为三级 : 档案拥有者、群组、其余 r 表示可读取,w 表示可写入,x 表示可执行 r=4,w=2,x=1 若要rwx属性则4+2+1=7; 若要rw-属性则4+2=6; 若要r-x属性则4+1=7 chmod ug=rwx,o=x path等同chmod 771 path 600 (只有全部者有读和写的权限) 644 (全部者有读和写的权限,组用户只有读的权限) 700 (只有全部者有读和写以及执行的权限) 666 (每一个人都有读和写的权限) 777(每一个人都有读和写以及执行的权限)
fs.link(srcpath, dstpath, callback)/ fs.linkSync fs.unlink(scrpath, dstpath, callback)/fs.unlinkSync
//建立 fs.symlink(srcpath, dstpath, callback)/ fs.symlinkSync //读取,不是有效文件符号连接会出错 fs.readlink(dstpath, callback)/ fs.readlinkSync
fs.realpath(path, [opetion], callback)/realpathSync
mkdir/mkdirSync
rmdir/rmdirSync
读取: readdir/readdirSync
返回文件目录数组数组
fs.exists(path, function (result) {})/fs.existsSync
读取文件内容app
//readFile/readFileSync fs.readFile(path, callback)/fs.readFileSync //read/readSync fs.open('1.txt', 'r', function (err, fd) { fs.fstat(fd, function (err, stat) { var buf = new Buffer(stat.size), bytesRead = 0; var length = fs.read(fd, buf, bytesRead, buf.length, null, function (err, bytesRead, buffer) { console.log(err, bytesRead, buffer.toString()); fs.close(fd); }); }) })
//writeFile/writeFileSync fs.writeFileSync(path, data); //write/writeSync fs.open('1.txt', 'w', function (err, fd) { var buf = new Buffer('aaa'); fs.write(fd, buf, 0, buf.length, null, function (err, bytesRead, buffer) { console.log(bytesRead, buffer.toString()); fs.close(fd); }) })
追加写文件: fs.appendFile/fs.appendFileSync
异步
监控文件: 注意这两个方法针对不一样的系统平台使用起来不是很稳定ui
fs.watchFile('1.txt', function (curr, prev) { console.log(curr, prev); }) fs.watch('1.txt', function (event, filename) { console.log(event); console.log(filename); })
path.normalize
过滤多余/
,处理./
,../
path.join
包括规范字符串路径处理path.resolve
: 包括规范和合并字符串路径path.relative
: 包括规范和合并字符串路径//根据已经获得的文件名称,得到该文件所在文件夹绝对路径 path.dirname(path.resolve('1.txt'))
path.extname
格式.xxx
path.basename('a/b/test.txt'); //c path.basename('a/b/test.txt', 't.txt'); //tex
url.parse url.format
url
路径上增长或替换标签url
是href
类型,则会插入或替换以最后一个/
为基础url
以/
开头,则插入或替换标签以/
开头会彻底替换console.log(url.resolve('/a/b', 'c')); // /a/c console.log(url.resolve('/a/b', '/c')); // /c console.log(url.resolve('/a/b/', 'c')); // /a/b/c console.log(url.resolve('/a/b/', '/c')); // /c console.log(url.resolve('http://xxx.com/a', 'c')); // http://xxx.com/c console.log(url.resolve('http://xxx.com/a', '/c')); // http://xxx.com/c console.log(url.resolve('http://xxx.com/a/', 'c')); // http://xxx.com/a/c console.log(url.resolve('http://xxx.com/a/', '/c')); // http://xxx.com/c
var querystring = require('querystring') querystring.parse(url)