三:fs模块

作者: 椰果粒 | 来源:发表于2018-06-18 17:25 被阅读13次

node的fs模块是文件模块,为node自带的模块,负责文件的读写。
fs模块可以同步和异步读写文件

1. 异步读取文件内容

// 严格模式
'use strict';
// 引入fs模块,因为我们只使用它而不修改,所以用cons
const fs = require('fs');
// 异步读取文件内容
// 传入回调函数,err是读取错误,data是读取到的字符串
// 这里"utf-8"指定了读取出来的格式,如果不加,读取出来的将是object类型
fs.readFile("index.html","utf8",function(err,data){
    if(err){
        console.log(err)
    }else{
        console.log(data)
    }
});

此结果是未加utf-8的结果:

<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 6c 61 6e 67 3d 22 65 6e 22 3e 0a 3c 68 65 61 64 3e 0a 20 20 20 20 3c 6d 65 74 61 20 ... >

注意:index.html必须与这个文件在同一目录下,文件编码为utf8

回调函数那里,当发生错误时,err是一个错误对象,data是undefined

node标准的回调函数,第一个参数是错误信息,第二个参数是结果

2. 同步读取文件内容

const content = fs.readFileSync("index.html","utf-8")
console.log(content)

3. 异步写文件

// 参数依次为:文件名,文件内容,异步的回调函数
fs.writeFile("test.txt",content, function(err){
    if(err){
        console.log(err)
    }else{
        console.log("ook")
    }
})

在写文件时,如果指定的文件名在目录下不存在,该文件将会被创建。如果已经存在了,该文件将被修改。

4. 同步写文件

fs.writeFileSync("test1.txt",content)

5. 如果想获取文件的详细信息,比如文件大小,是否为文件等,用fs.stat

// stat对象
fs.stat("index.html", function(err,stat){
    if(err){
        console.log(err)
    }else{
        // 是否是文件
        console.log("isFile:"+stat.isFile())
        // 是否是目录
        console.log("isDirectory:"+stat.isDirectory())
        // 创建时间
        console.log("birthtime:"+stat.birthtime)
        // 修改时间
        console.log("modified time"+stat.mtime)
        console.log(stat)
    }
})

结果:

isFile:true
isDirectory:false
birthtime:Wed Jun 06 2018 19:56:45 GMT+0800 (CST)
modified timeWed Jun 06 2018 19:56:45 GMT+0800 (CST)

Stats {
  dev: 16777220,
  mode: 33188,
  nlink: 1,
  uid: 501,
  gid: 20,
  rdev: 0,
  blksize: 4194304,
  ino: 8597998263,
  size: 385,
  blocks: 8,
  atime: 2018-06-18T09:18:16.669Z,
  mtime: 2018-06-06T11:56:45.033Z,
  ctime: 2018-06-06T11:56:45.035Z,
  birthtime: 2018-06-06T11:56:45.032Z }

6. 同步文件的stat

const statSync = fs.statSync("index.html","utf-8");
// 是否是文件
console.log("isFile:"+statSync.isFile())
// 是否是目录
console.log("isDirectory:"+statSync.isDirectory())
// 创建时间
console.log("birthtime:"+statSync.birthtime)
// 修改时间
console.log("modified time"+statSync.mtime)

结果:

isFile:true
isDirectory:false
birthtime:Wed Jun 06 2018 19:56:45 GMT+0800 (CST)
modified timeWed Jun 06 2018 19:56:45 GMT+0800 (CST)

7. 使用同步还是异步
一定要使用异步,因为读取写入文件是比较耗费时间的,所以无论是读还是写,都要用异步的方式。

相关文章

  • Node基础备注

    模块 http模块 url模块 fs模块 fs.state:检测是文件还是目录fs.mkdir:创建目录fs.wr...

  • node的fs模块

    一.fs模块的同步和异步 二.fs模块读取异步 三.fs模块写入文件内容(如果没有会创建文件,写入时会清空文件,f...

  • 三:fs模块

    node的fs模块是文件模块,为node自带的模块,负责文件的读写。fs模块可以同步和异步读写文件 1. 异步读取...

  • nodeJS常用模块

    模块一:assert 模块二:path 模块三:fs 模块四:http 模块五:url和queryString

  • node栈-第三方工具-文件类

    fs-extra fs-extra模块是系统fs模块的扩展,提供了更多便利的 API,并继承了fs模块的 API ...

  • NodeJS常用API

    一、http模块: 二、NodeJS的模块: 三、 fs模块 四、全局变量 五、path模块: 六、 mime模块...

  • node.js文件操作

    fs 文件操作用的模块为node.js中的fs模块.因此要提前将其引入. var fs = require('fs...

  • fs的核心模块及方法

    fs的核心模块及方法 fs模块(http://nodejs.cn/api/)

  • 周国康-20160809笔记

    HTTP,URL,FS模块 HTTP模块 FS模块 URL模块 作业 构造静态服务Server:解析URL,根据U...

  • 八:Node文件系统管理

    导入:var fs = require("fs") 定义:Node.js 文件系统(fs 模块)模块中的方法均有异...

网友评论

    本文标题:三:fs模块

    本文链接:https://www.haomeiwen.com/subject/wolmeftx.html