return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
以上代码执行结果如下:
$ node file.js
查看 /tmp 目录
input.out
output.out
test
test.txt
删除目录
语法
以下为删除目录的语法格式:
fs.rmdir(path, callback)
参数
参数使用说明如下:
path – 文件路径。
callback – 回调函数,没有参数。
实例
接下来我们创建 file.js 文件,代码如下所示:
var fs = require("fs");
// 执行前创建一个空的 /tmp/test 目录
console.log("准备删除目录 /tmp/test");
fs.rmdir("/tmp/test",function(err){
if (err) {
return console.error(err);
}
console.log("读取 /tmp 目录");
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
});
以上代码执行结果如下:
$ node file.js
准备删除目录 /tmp/test
读取 /tmp 目录
……
文件模块方法参考手册
以下为 Node.js 文件模块相同的方法列表:
| 序号 | 方法 & 描述 |
|---|---|
| 1 | fs.rename(oldPath, newPath, callback) 异步 rename().回调函数没有参数,但可能抛出异常。 |
| 2 | fs.ftruncate(fd, len, callback) 异步 ftruncate().回调函数没有参数,但可能抛出异常。 |
| 3 | fs.ftruncateSync(fd, len) 同步 ftruncate() |
| 4 | fs.truncate(path, len, callback) 异步 truncate().回调函数没有参数,但可能抛出异常。 |
| 5 | fs.truncateSync(path, len) 同步 truncate() |
| 6 | fs.chown(path, uid, gid, callback) 异步 chown().回调函数没有参数,但可能抛出异常。 |
| 7 | fs.chownSync(path, uid, gid) 同步 chown() |
| 8 | fs.fchown(fd, uid, gid, callback) 异步 fchown().回调函数没有参数,但可能抛出异常。 |
| 9 | fs.fchownSync(fd, uid, gid) 同步 fchown() |
| 10 | fs.lchown(path, uid, gid, callback) 异步 lchown().回调函数没有参数,但可能抛出异常。 |
| 11 | fs.lchownSync(path, uid, gid) 同步 lchown() |
| 12 | fs.chmod(path, mode, callback)
相关文章
大家在看
|









