您的位置:首页 > Web前端 > Node.js

nodejs 之 file system 文件系统

2012-08-17 11:36 399 查看
var fs = require('fs');

/**

先解释有函数如:unlink()和unlinkSync(),它们的功能是一样的

只是unlink是同步类型,因为它最后那个参数是回调函数,而unlinkSync参数中是没有回调函数的

两个方法运用比较如下:

1,fs.unlink('/tmp/hello', function (err) {

if (err) throw err;

console.log('successfully deleted /tmp/hello');

});

2,fs.unlinkSync('/tmp/hello')

console.log('successfully deleted /tmp/hello');

同步类型的方法并没有固定的运行顺序,如下面的代码就可能出错:

fs.rename('/tmp/hello', '/tmp/world', function (err) {

if (err) throw err;

console.log('renamed complete');

});

fs.stat('/tmp/world', function (err, stats) {

if (err) throw err;

console.log('stats: ' + JSON.stringify(stats));

});

fs.stat很可能在fs.rename之前被执行。下面是正确的方法:

fs.rename('/tmp/hello', '/tmp/world', function (err) {

if (err) throw err;

fs.stat('/tmp/world', function (err, stats) {

if (err) throw err;

console.log('stats: ' + JSON.stringify(stats));

});

});

在很繁忙进程中,强烈建议使用异步类型的函数。因同步的,在它没有运行完之前,将阻塞整个进程

*/

/** fs.rename(oldPath, newPath, [callback])

*/

/** fs.truncate(fd, len, [callback])

*/

/** fs.chown(path, uid, gid, [callback])

fs.fchown(fd, uid, gid, [callback])

fs.lchown(path, uid, gid, [callback])

*/

/** fs.chmod(path, mode, [callback])

fs.fchmod(fd, mode, [callback])

fs.lchmod(path, mode, [callback])

*/

/** fs.stat(path, [callback])

fs.lstat(path, [callback])

fs.fstat(fd, [callback]) fd file descriptor文件说明符

它们的回调函数拥有两个参数(err,stats),stats为fs.Stats对象

•stats.isFile()

•stats.isDirectory()

•stats.isBlockDevice()

•stats.isCharacterDevice()

•stats.isSymbolicLink() (only valid with fs.lstat())

•stats.isFIFO()

•stats.isSocket()

*/

/** fs.link(srcpath, dstpath, [callback])

fs.symlink(srcpath, dstpath, [type], [callback])

type参数值为dir、file或junction.只适用于Windows,其他平台type被忽略

当用的是junctions时,dstpath(destination path目标路径)要为绝对路径

dstpath将会被自动标准化(normalized)为绝对路径

fs.readlink(path, [callback])

回调函数拥有两个参数(err, linkString)

*/

/** fs.realpath(path, [cache], callback)

回调函数拥有两个参数(err, resolvedPath)

*/

var cache = {'/etc':'/private/etc'};

fs.realpath(process.cwd() +'/etc/passwd', cache, function(err, resolvedPath){

if(err) throw err;

console.log(resolvedPath);

});

/** fs.unlink(path, [callback])

*/

/** fs.rmdir(path, [callback]) 删除目录

fs.mkdir(path, [mode], [callback]) 创建目录

fs.readdir(path, [callback])

回调函数拥有两个参数(err, files) files为path路径下文件名的数组

*/

/** fs.close(fd, [callback])

*/

/** fs.open(path, flags, [mode], [callback])

回调函数拥有两个参数(err, fd)

*/

/** fs.write(fd, buffer, offset, length, position, [callback])

回调函数拥有三个参数(err, written, buffer)

buffer写到fd文件的内容,offset,length为从offset位置开始写length长的内容(buffer)

position为从fd文件position位置开始把内容写进去

*/

/** fs.read(fd, buffer, offset, length, position, [callback])

在buffer的offset位置开始写入fd文件中position位置开始的内容,内容长度为length

回调函数拥有三个参数(err, bytesRead, buffer).

*/

/** fs.readFile(filename, [encoding], [callback])

读取文件的全部内容

回调函数拥有两个参数(err, data).data为文件的内容

*/

fs.readFile('/etc/passwd', function (err, data) {

if (err) throw err;

console.log(data);

});

/** fs.writeFile(filename, data, [encoding], [callback])

将data数据写入到filename中

*/

fs.writeFile('message.txt', 'Hello Node', function (err) {

if (err) throw err;

console.log('It\'s saved!');

});

/** fs.appendFile(filename, data, encoding='utf8', [callback])

将data数据添加到filename文件中,文件不存在会自动创建

当data为buffer时,encoding忽略不写,data可以为string或buffer

*/

fs.appendFile('message.txt', 'data to append', function (err) {

if (err) throw err;

console.log('The "data to append" was appended to file!');

});

/** fs.watchFile(filename, [options], listener) //不稳定,如果可用,用fs.watch()

监视文件,listener回调函数在每次文件被使用时触发

options有两个选项:persistent 布尔值--文件被监视进程是否再继续,

interval 时间(单位毫秒)--文件被多长时间被查询一次

fs.unwatchFile(filename, [listener]) //不稳定,如果可用,用fs.watch()

fs.watch(filename, [options], [listener]) //不稳定,不是所有的平台都适用

*/

fs.watchFile('message.text', function (curr, prev) {

console.log('the current mtime is: ' + curr.mtime);

console.log('the previous mtime was: ' + prev.mtime);

});

fs.watch('somedir', function (event, filename) { //目前只有linux和windows平台提供filename参数

console.log('event is: ' + event);

if (filename) {

console.log('filename provided: ' + filename);

} else {

console.log('filename not provided');

}

});

/** fs.exists(path, [callback])

判断path是否存在

*/

fs.exists('/etc/passwd', function (exists) {

util.debug(exists ? "it's there" : "no passwd!");

});

/** fs.createReadStream(path, [options])

{ flags: 'r',

encoding: null, //'utf8', 'ascii', or 'base64'

fd: null,

mode: 0666,

bufferSize: 64 * 1024

} 也有start,end表示从什么位置开始读,到什么位置结束

*/

/** fs.createWriteStream(path, [options])

{ flags: 'w',

encoding: null,

mode: 0666 }也包括start允许从什么位置开始写入数据

*/

/** fs.utimes(path, atime, mtime, [callback])

更改path路径下的文件的文件时间戳

fs.futimes(fd, atime, mtime, [callback])

更改有关该文件说明符的文件的文件时间戳

*/

/** fs.fsync(fd, [callback])

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: