Node 文件操作,缓存区 Buffer 理解 ?

  • Node 读取文件 ?

1,Node 读取文件普通用法;

// 1,导入文件模块
const fs = require('fs')
// node 读写文件有同步和异步的接口, 默认为异步
// 同步 Sync
const content = fs.readFileSync('hello.txt', { flag: 'r', encoding: 'utf-8' })
console.log('同步读取文件:', content)
// 异步
fs.readFile('hello.txt', { flag: 'r', encoding: 'utf-8' }, function(err, data) {
        err ? console.log(err) : console.log('异步读取文件:', data)
})

2,Node 读取文件 promise 封装;

const fs = require('fs')
 // promise 封装
function fsRead(path) {
    return new Promise(function(resolve, reject) {
        fs.readFile(path, { flag: 'r', encoding: 'utf-8' }, function(err, data) {
            if (err) {
                // 失败执行的内容
                reject(err)
            } else {
                // 成功执行的内容
                resolve(data)
            }
        })
    })
}

var fs1 = fsRead('hello.txt')
fs1.then(function(res) {
    console.log('promise 封装异步读取文件:', res)
})

3,Node 读取文件 async 和 await 简化操作;(推荐)

const fs = require('fs')
// promise 封装
function fsRead(path) {
    return new Promise(function(resolve, reject) {
        fs.readFile(path, { flag: 'r', encoding: 'utf-8' }, function(err, data) {
            if (err) {
                // 失败执行的内容
                reject(err)
            } else {
                // 成功执行的内容
                resolve(data)
            }
        })
    })
}
// 顺序执行
async function ReadList() {
    var content = await fsRead('hello.txt')
    var content1 = await fsRead('hello1.txt')
    console.log('async 和 await 简化:', content)
}

ReadList()
  • Node 写入文件 ?

1,Node 写入文件普通用法;

const fs = require('fs')
fs.writeFile('content.txt', '我很喜欢你!', { flag: 'a', encoding: 'utf-8' }, function(err) {
    err ? console.log('写入失败!!!', err) : console.log('写入内容成功!')
})

2,Node 写入文件 promise 封装;

const fs = require('fs')
function WriteFile(path, data) {
    return new Promise(function(resolve, reject) {
        fs.writeFile(path, data, { flag: 'a', encoding: 'utf-8' }, function(err) {
            if (err) {
                reject(err)
            } else {
                resolve()
            }
        })
    })
}
WriteFile('content.txt', '我也喜欢你!\n').then(function() {
    console.log('写入成功!!!')
})

3,Node 写入文件 async 和 await 简化操作;(推荐)

const fs = require('fs')
function WriteFile(path, data) {
    return new Promise(function(resolve, reject) {
        fs.writeFile(path, data, { flag: 'a', encoding: 'utf-8' }, function(err) {
            if (err) {
                reject(err)
            } else {
                resolve()
            }
        })
    })
}
async function WriteList() {
    await WriteFile('content.txt', '这是一个悲伤的故事 \n')
    await WriteFile('content.txt', '我喜欢你 \n')
    await WriteFile('content.txt', '但是你不喜欢我 \n')
    await WriteFile('content.txt', 'OMG !!! \n')
}

WriteList()
  • Node 删除文件 ?

Node 删除文件用法 :fs.unlink(path,callback) ,删除文件;(谨慎使用)

const fs = require('fs')
fs.unlink('../08_文件写入/content.txt', function() {
    console.log('删除文件成功!!!')
})
  • Buffer 简介 ?

JS 数组不能进行二进制数据的操作。不像 java, python 等语言有确定的字节大小,变量内存不一定连续,所以效率不高。为了 提高数组性能,出现 buffer,会在内存空间开辟出固定大小的内存(连续)

1,将字符串转成 buffer;

const fs = require('fs')
const str = 'hello,world'
const buf = Buffer.from(str)
console.log(buf) // 本质二进制,十六进制表示

2,输出 buffer 实际内容;

const str = 'hello,world'
const buf = Buffer.from(str)
// 输出 buffer 内容
console.log(buf.toString()) // 'hello,world'

3,开辟一个空的 buffer (缓存区);

// 开辟一个空的 buffer (缓存区)
// 开辟10个字节,清空原本内容
let buf1 = Buffer.alloc(10) 
buf1[0] = 10  // 0a
buf1[1] = 255 // ff
buf1[2] = 256 // 00 超出8位,100000000,省略前面1
console.log(buf1)
// 开辟10个字节,不会清空原本内容
let buf2 = Buffer.allocUnsafe(10) 
console.log(buf2)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值