“Node.js:文件系统”的版本间差异
跳到导航
跳到搜索
(建立内容为“category:Node.js == 关于 == Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API。 Node 导入文件系统模块(fs)语法如下所示: ''…”的新页面) |
无编辑摘要 |
||
(未显示同一用户的2个中间版本) | |||
第1行: | 第1行: | ||
[[category:Node. | [[category:Node.js教程]] | ||
== 关于 == | == 关于 == | ||
Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API。 Node 导入文件系统模块(fs)语法如下所示: | Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API。 Node 导入文件系统模块(fs)语法如下所示: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs") | var fs = require("fs") | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第20行: | 第20行: | ||
示例: | 示例: | ||
# 创建 input.txt 文件,内容如下: | # 创建 input.txt 文件,内容如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
菜鸟教程官网地址:www.runoob.com | 菜鸟教程官网地址:www.runoob.com | ||
文件读取实例 | 文件读取实例 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 创建 file.js 文件, 代码如下: | # 创建 file.js 文件, 代码如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
第43行: | 第43行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
同步读取: 菜鸟教程官网地址:www.runoob.com | 同步读取: 菜鸟教程官网地址:www.runoob.com | ||
第56行: | 第56行: | ||
以下为在'''异步'''模式下的语法格式。 | 以下为在'''异步'''模式下的语法格式。 | ||
=== | === 打开文件:fs.open === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.open(path, flags[, mode], callback) | fs.open(path, flags[, mode], callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第100行: | 第100行: | ||
示例: | 示例: | ||
# 创建 file.js 文件,并打开 input.txt 文件进行读写: | # 创建 file.js 文件,并打开 input.txt 文件进行读写: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
第113行: | 第113行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备打开文件! | 准备打开文件! | ||
第119行: | 第119行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 获取文件信息:fs.stat === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.stat(path, callback) | fs.stat(path, callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第152行: | 第152行: | ||
示例: | 示例: | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
第169行: | 第169行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备打开文件! | 准备打开文件! | ||
第190行: | 第190行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 读取文件:fs.read === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.read(fd, buffer, offset, length, position, callback) | fs.read(fd, buffer, offset, length, position, callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第207行: | 第207行: | ||
示例: | 示例: | ||
# input.txt 文件内容为: | # input.txt 文件内容为: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
菜鸟教程官网地址:www.runoob.com | 菜鸟教程官网地址:www.runoob.com | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
var buf = new Buffer.alloc(1024); | var buf = new Buffer.alloc(1024); | ||
第236行: | 第236行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备打开已存在的文件! | 准备打开已存在的文件! | ||
第245行: | 第245行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 写入文件:fs.writeFile === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.writeFile(file, data[, options], callback) | fs.writeFile(file, data[, options], callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第262行: | 第262行: | ||
示例: | 示例: | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
第282行: | 第282行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备写入文件 | 准备写入文件 | ||
第291行: | 第291行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 关闭文件:fs.close === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.close(fd, callback) | fs.close(fd, callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第304行: | 第304行: | ||
示例: | 示例: | ||
# input.txt 文件内容为: | # input.txt 文件内容为: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
菜鸟教程官网地址:www.runoob.com | 菜鸟教程官网地址:www.runoob.com | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
var buf = new Buffer.alloc(1024); | var buf = new Buffer.alloc(1024); | ||
第340行: | 第340行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备打开文件! | 准备打开文件! | ||
第349行: | 第349行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 截取文件:fs.ftruncate === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.ftruncate(fd, len, callback) | fs.ftruncate(fd, len, callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第365行: | 第365行: | ||
示例: | 示例: | ||
# input.txt 文件内容为: | # input.txt 文件内容为: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
site:www.runoob.com | site:www.runoob.com | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
var buf = new Buffer.alloc(1024); | var buf = new Buffer.alloc(1024); | ||
第410行: | 第410行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备打开文件! | 准备打开文件! | ||
第421行: | 第421行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 删除文件:fs.unlink === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.unlink(path, callback) | fs.unlink(path, callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第434行: | 第434行: | ||
示例: | 示例: | ||
# input.txt 文件内容为: | # input.txt 文件内容为: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
site:www.runoob.com | site:www.runoob.com | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
第450行: | 第450行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备删除文件! | 准备删除文件! | ||
第456行: | 第456行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 创建目录:fs.mkdir === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.mkdir(path[, options], callback) | fs.mkdir(path[, options], callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第472行: | 第472行: | ||
示例: | 示例: | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
// tmp 目录必须存在 | // tmp 目录必须存在 | ||
第484行: | 第484行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
创建目录 /tmp/test/ | 创建目录 /tmp/test/ | ||
第490行: | 第490行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* 可以添加 recursive: true 参数,不管创建的目录 /tmp 和 /tmp/a 是否存在: | * 可以添加 recursive: true 参数,不管创建的目录 /tmp 和 /tmp/a 是否存在: | ||
*: <syntaxhighlight lang=" | *: <syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => { | fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => { | ||
if (err) throw err; | if (err) throw err; | ||
第496行: | 第496行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 读取目录:fs.readdir === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.readdir(path, callback) | fs.readdir(path, callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第511行: | 第511行: | ||
示例: | 示例: | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
第525行: | 第525行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
查看 /tmp 目录 | 查看 /tmp 目录 | ||
第534行: | 第534行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === 删除目录:fs.rmdir === | ||
语法: | 语法: | ||
'''<syntaxhighlight lang=" | '''<syntaxhighlight lang="JavaScript" highlight=""> | ||
fs.rmdir(path, callback) | fs.rmdir(path, callback) | ||
</syntaxhighlight>''' | </syntaxhighlight>''' | ||
第547行: | 第547行: | ||
示例: | 示例: | ||
# 创建 file.js 文件: | # 创建 file.js 文件: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
var fs = require("fs"); | var fs = require("fs"); | ||
第570行: | 第570行: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# 以上代码执行结果如下: | # 以上代码执行结果如下: | ||
#: <syntaxhighlight lang=" | #: <syntaxhighlight lang="JavaScript" highlight=""> | ||
$ node file.js | $ node file.js | ||
准备删除目录 /tmp/test | 准备删除目录 /tmp/test |
2023年3月31日 (五) 21:21的最新版本
关于
Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API。 Node 导入文件系统模块(fs)语法如下所示:
var fs = require("fs")
异步和同步
Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本。
- 例如读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFileSync()。
异步的方法函数最后一个参数为回调函数,回调函数的第一个参数包含了错误信息(error)。
建议使用异步方法,比起同步,异步方法性能更高,速度更快,而且没有阻塞。
示例:
- 创建 input.txt 文件,内容如下:
菜鸟教程官网地址:www.runoob.com 文件读取实例
- 创建 file.js 文件, 代码如下:
var fs = require("fs"); // 异步读取 fs.readFile('input.txt', function (err, data) { if (err) { return console.error(err); } console.log("异步读取: " + data.toString()); }); // 同步读取 var data = fs.readFileSync('input.txt'); console.log("同步读取: " + data.toString()); console.log("程序执行完毕。");
- 以上代码执行结果如下:
$ node file.js 同步读取: 菜鸟教程官网地址:www.runoob.com 文件读取实例 程序执行完毕。 异步读取: 菜鸟教程官网地址:www.runoob.com 文件读取实例
用法
以下为在异步模式下的语法格式。
打开文件:fs.open
语法:
fs.open(path, flags[, mode], callback)
参数:
- path - 文件的路径。
- flags - 文件打开的行为。
- mode - 设置文件模式(权限),文件创建默认权限为 0666(可读,可写)。
- callback - 回调函数,带有两个参数如:callback(err, fd)。
flags 参数可以是以下值:
Flag | 描述 |
---|---|
r | 以读取模式打开文件。如果文件不存在抛出异常。 |
r+ | 以读写模式打开文件。如果文件不存在抛出异常。 |
rs | 以同步的方式读取文件。 |
rs+ | 以同步的方式读取和写入文件。 |
w | 以写入模式打开文件,如果文件不存在则创建。 |
wx | 类似 'w',但是如果文件路径存在,则文件写入失败。 |
w+ | 以读写模式打开文件,如果文件不存在则创建。 |
wx+ | 类似 'w+', 但是如果文件路径存在,则文件读写失败。 |
a | 以追加模式打开文件,如果文件不存在则创建。 |
ax | 类似 'a', 但是如果文件路径存在,则文件追加失败。 |
a+ | 以读取追加模式打开文件,如果文件不存在则创建。 |
ax+ | 类似 'a+', 但是如果文件路径存在,则文件读取追加失败。 |
示例:
- 创建 file.js 文件,并打开 input.txt 文件进行读写:
var fs = require("fs"); // 异步打开文件 console.log("准备打开文件!"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("文件打开成功!"); });
- 以上代码执行结果如下:
$ node file.js 准备打开文件! 文件打开成功!
获取文件信息:fs.stat
语法:
fs.stat(path, callback)
参数:
- path - 文件路径。
- callback - 回调函数,带有两个参数如:(err, stats), stats 是 fs.Stats 对象。
fs.stat(path) 执行后,会将 stats类 的实例返回给其回调函数:
stats类方法 | 描述 |
---|---|
stats.isFile() | 如果是文件返回 true,否则返回 false。 |
stats.isDirectory() | 如果是目录返回 true,否则返回 false。 |
stats.isBlockDevice() | 如果是块设备返回 true,否则返回 false。 |
stats.isCharacterDevice() | 如果是字符设备返回 true,否则返回 false。 |
stats.isSymbolicLink() | 如果是软链接返回 true,否则返回 false。 |
stats.isFIFO() | 如果是FIFO,返回true,否则返回 false。
|
stats.isSocket() | 如果是 Socket 返回 true,否则返回 false。 |
示例:
- 创建 file.js 文件:
var fs = require("fs"); console.log("准备打开文件!"); fs.stat('input.txt', function (err, stats) { if (err) { return console.error(err); } console.log(stats); console.log("读取文件信息成功!"); // 检测文件类型 console.log("是否为文件(isFile) ? " + stats.isFile()); console.log("是否为目录(isDirectory) ? " + stats.isDirectory()); });
- 以上代码执行结果如下:
$ node file.js 准备打开文件! { dev: 16777220, mode: 33188, nlink: 1, uid: 501, gid: 20, rdev: 0, blksize: 4096, ino: 40333161, size: 61, blocks: 8, atime: Mon Sep 07 2015 17:43:55 GMT+0800 (CST), mtime: Mon Sep 07 2015 17:22:35 GMT+0800 (CST), ctime: Mon Sep 07 2015 17:22:35 GMT+0800 (CST) } 读取文件信息成功! 是否为文件(isFile) ? true 是否为目录(isDirectory) ? false
读取文件:fs.read
语法:
fs.read(fd, buffer, offset, length, position, callback)
参数:
- fd - 通过 fs.open() 方法返回的文件描述符。
- buffer - 数据写入的缓冲区。
- offset - 缓冲区写入的写入偏移量。
- length - 要从文件中读取的字节数。
- position - 文件读取的起始位置,如果 position 的值为 null,则会从当前文件指针的位置读取。
- callback - 回调函数,有三个参数:err, bytesRead, buffer;其中 err 为错误信息, bytesRead 表示读取的字节数,buffer 为缓冲区对象。
示例:
- input.txt 文件内容为:
菜鸟教程官网地址:www.runoob.com
- 创建 file.js 文件:
var fs = require("fs"); var buf = new Buffer.alloc(1024); console.log("准备打开已存在的文件!"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("文件打开成功!"); console.log("准备读取文件:"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if (err){ console.log(err); } console.log(bytes + " 字节被读取"); // 仅输出读取的字节 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } }); });
- 以上代码执行结果如下:
$ node file.js 准备打开已存在的文件! 文件打开成功! 准备读取文件: 42 字节被读取 菜鸟教程官网地址:www.runoob.com
写入文件:fs.writeFile
语法:
fs.writeFile(file, data[, options], callback)
- writeFile 直接打开文件默认是 w 模式,所以如果文件存在,该方法写入的内容会覆盖旧的文件内容。
参数:
- file - 文件名或文件描述符。
- data - 要写入文件的数据,可以是 String(字符串) 或 Buffer(缓冲) 对象。
- options - 该参数是一个对象,包含 {encoding, mode, flag}。
- 默认编码为 utf8,模式为 0666,flag 为 'w'
- callback - 回调函数,回调函数只包含错误信息参数(err),在写入失败时返回。
示例:
- 创建 file.js 文件:
var fs = require("fs"); console.log("准备写入文件"); fs.writeFile('input.txt', '我是通过 fs.writeFile 写入文件的内容', function(err) { if (err) { return console.error(err); } console.log("数据写入成功!"); console.log("--------我是分割线-------------") console.log("读取写入的数据!"); fs.readFile('input.txt', function (err, data) { if (err) { return console.error(err); } console.log("异步读取文件数据: " + data.toString()); }); });
- 以上代码执行结果如下:
$ node file.js 准备写入文件 数据写入成功! --------我是分割线------------- 读取写入的数据! 异步读取文件数据: 我是通过 fs.writeFile 写入文件的内容
关闭文件:fs.close
语法:
fs.close(fd, callback)
参数:
- fd - 通过 fs.open() 方法返回的文件描述符。
- callback - 回调函数,没有参数(仅有参数 err)。
示例:
- input.txt 文件内容为:
菜鸟教程官网地址:www.runoob.com
- 创建 file.js 文件:
var fs = require("fs"); var buf = new Buffer.alloc(1024); console.log("准备打开文件!"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("文件打开成功!"); console.log("准备读取文件!"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if (err){ console.log(err); } // 仅输出读取的字节 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } // 关闭文件 fs.close(fd, function(err){ if (err){ console.log(err); } console.log("文件关闭成功"); }); }); });
- 以上代码执行结果如下:
$ node file.js 准备打开文件! 文件打开成功! 准备读取文件! 菜鸟教程官网地址:www.runoob.com 文件关闭成功
截取文件:fs.ftruncate
语法:
fs.ftruncate(fd, len, callback)
- 该方法使用了文件描述符来读取文件;
- 截取:将源文件截取覆盖到源文件;
参数:
- fd - 通过 fs.open() 方法返回的文件描述符。
- len - 文件内容截取的长度。
- callback - 回调函数,没有参数。
示例:
- input.txt 文件内容为:
site:www.runoob.com
- 创建 file.js 文件:
var fs = require("fs"); var buf = new Buffer.alloc(1024); console.log("准备打开文件!"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("文件打开成功!"); console.log("截取10字节内的文件内容,超出部分将被去除。"); // 截取文件 fs.ftruncate(fd, 10, function(err){ if (err){ console.log(err); } console.log("文件截取成功。"); console.log("读取相同的文件"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if (err){ console.log(err); } // 仅输出读取的字节 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } // 关闭文件 fs.close(fd, function(err){ if (err){ console.log(err); } console.log("文件关闭成功!"); }); }); }); });
- 以上代码执行结果如下:
$ node file.js 准备打开文件! 文件打开成功! 截取10字节内的文件内容,超出部分将被去除。 文件截取成功。 读取相同的文件 site:www.r 文件关闭成功
删除文件:fs.unlink
语法:
fs.unlink(path, callback)
参数:
- path - 文件路径。
- callback - 回调函数,没有参数。
示例:
- input.txt 文件内容为:
site:www.runoob.com
- 创建 file.js 文件:
var fs = require("fs"); console.log("准备删除文件!"); fs.unlink('input.txt', function(err) { if (err) { return console.error(err); } console.log("文件删除成功!"); });
- 以上代码执行结果如下:
$ node file.js 准备删除文件! 文件删除成功!
创建目录:fs.mkdir
语法:
fs.mkdir(path[, options], callback)
参数:
- path - 文件路径。
- options 参数可以是:
- recursive - 是否以递归的方式创建目录,默认为 false。
- mode - 设置目录权限,默认为 0777。
- callback - 回调函数,没有参数。
示例:
- 创建 file.js 文件:
var fs = require("fs"); // tmp 目录必须存在 console.log("创建目录 /tmp/test/"); fs.mkdir("/tmp/test/",function(err){ if (err) { return console.error(err); } console.log("目录创建成功。"); });
- 以上代码执行结果如下:
$ node file.js 创建目录 /tmp/test/ 目录创建成功。
- 可以添加 recursive: true 参数,不管创建的目录 /tmp 和 /tmp/a 是否存在:
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => { if (err) throw err; });
读取目录:fs.readdir
语法:
fs.readdir(path, callback)
参数:
- path - 文件路径。
- callback - 回调函数,回调函数带有两个参数:
- err 为错误信息,
- files 为目录下的文件数组列表。
示例:
- 创建 file.js 文件:
var fs = require("fs"); 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 目录 input.out output.out test test.txt
删除目录:fs.rmdir
语法:
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 文件模块相同的方法列表:见“https://nodejs.org/api/fs.html#fs_fs_rename_oldpath_newpath_callback”。