P06:node内置模块 fs(1)

575 阅读3分钟

相关文章

fs 文件系统

简单介绍

fs模块是node.js的内置模块

fs 模块提供了一个 API,用于以模仿标准 POSIX 函数的方式与文件系统进行交互

需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。

读文件

```
const fs = require('fs')
// 需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。
fs.readFile('./06_fs.js', (err, data) => {
    //  如果出现错误 通过throw输出
    if (err) throw err
    console.log(data) 
})
// 打印 二进制 一个buf
/**
 * PS E:\xxx\ccc\aaa\api> node 06_fs.js
    <Buffer 2f 2f 20 e6 89 80 e6 9c 89 e6 96 87
    e4 bb b6 e7 b3 bb e7 bb 9f e6 93 8d e4 bd 9c
    e9 83 bd e5 85 b7 e6 9c 89 e5 90 8c e6 ad a5
    e5 92 8c e5 bc 82 e6 ad ... >
 */
```

写文件

```
//  写一个文件

const fs = require('fs')

fs.writeFile('./test.js', 'test test', {
    encoding: 'utf8'
}, (err) => {
    if (err) throw err
    console.log('done!')
})

//  打印 
// 在本文件价下增加一个test.js文件 内容为 test test
// done! 
```

输出为特定格式 例如字符串

  • toString()
    const fs = require('fs')
    // 需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。
    fs.readFile('./07_fs_throw_string.js', (err, data) => {
        //  如果出现错误 通过throw输出
        if (err) throw err
        console.log(data)
        // 输出字符串
        console.log(data.toString()) 
    })
    <!--打印 -->
    /**
     *  PS E:\xxx\xxx\xxx\api> node 07_fs_throw_string.js
        <Buffer 2f 2f 20 20 e8 be 93 e5 87 ba 20 e5 
        ad 97 e7 ac a6 e4 b8 b2 0d 0a 63 6f 6e 73 74 
        20 66 73 20 3d 20 72 65 71 75 69 72 65 28 27
        66 73 27 29 0d 0a 2f 2f ... >
        //  输出 字符串
        const fs = require('fs')
        // 需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。
        fs.readFile('./07_fs_throw_string.js', (err, data) => {
            //  如果出现错误 通过throw输出
            if (err) throw err
            console.log(data)
            // 输出字符串
            console.log(data.toString())
        })
     */
    
  • 传入参数~ 推荐
    const fs = require('fs')
    // 需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。
    fs.readFile('./08_fs_throw_string.js', 'utf8', (err, data) => {
        //  如果出现错误 通过throw输出
        if (err) throw err
        console.log(data)
    })
    
    // 输出
    // const fs = require('fs')
    // // 需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。
    // fs.readFile('./08_fs_throw_string.js', 'utf8', (err, data) => {
    //     //  如果出现错误 通过throw输出
    //     if (err) throw err
    //     console.log(data)
    // })
    

异步同步

两者都需要读取完文件(或者部分读取)才能返回文件,都是需要等待的,那么两者的区别什么呢 ?

  • 对于单独用户使用服务器来说是没有区别的,或者说区别不明显。
  • 但是web实际场景中是大量的用户在使用,同步会导致前面的用户处理阻挡后面的用户处理。
  • 异步则会直接在让其单独等待,继续处理其他,实现了高并发。
  • 另外所有的api 都有同步的版本,详见node
  • 举个例子
    // 异步
    const fs = require('fs')
    // 需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。
    fs.readFile('./08_fs_throw_string.js', 'utf8', (err, data) => {
        //  如果出现错误 通过throw输出
        if (err) throw err
        console.log(111111111)
    })
    
    //  同步
    const data = fs.readFileSync('./09_fs_sync.js', 'utf8')
    
    console.log(data)
    
    // 打印
    /**
     * // 异步
        const fs = require('fs')
        // 需要注意一点 如果操作成功完成,则第一个参数将为 null 或 undefined。
        fs.readFile('./08_fs_throw_string.js', 'utf8', (err, data) => {
            //  如果出现错误 通过throw输出
            if (err) throw err
            console.log(111111111)
        })
    
        //  同步
        const data = fs.readFileSync('./09_fs_sync.js', 'utf8')
    
        console.log(data)
        111111111
     * 
     */
    

    可以看到同步的方法即使写在后面也会先执行,因为异步的方法是交给i/o去执行的

close