nodejs模块fs——文件操作api

// fs模块常用api

//  读取文件 、写入文件 、追加文件、 拷贝文件 、删除文件

//  读取文件
//  fs.readFile(path[, options], callback)
//  fs.readFileSync(path[, options])

const fs = require('fs')
//  异步读取
fs.readFile('./test.json', (error, data) => {
    if (error) return
    var data = data.toString()
    console.log(data)
})
//  同步读取
try {
    let data = fs.readFileSync('./test.json','utf-8')
    console.log(data)
} catch (error) {
    console.log(error)
}

//  文件写入
//  fs.writeFile(file, data[, options], callback)
//  fs.writeFileSync(file, data[, options])

//  异步写入
fs.writeFile('./data/datalist.json','{"data":"测试数据"}', error => {
        if (error) {
            console.log(error)
        }
    })
//  同步写入
try {
    fs.writeFileSync('./data/datalist.json','{"data":"测试数据2"}')
} catch (error) {
    console.log(error)
}

//  文件追加
//  fs.appendFile(path, data[, options], callback)
//  fs.appendFileSync(path, data[, options], callback)

//  异步追加
fs.appendFile('./data/datalist.json','追加数据',err=>{
   if(err) console.log(err)
})
// //  同步追加
try {
    fs.appendFileSync('./data/datalist.json','同步追加')
} catch (error) {
    console.log(error);
}

//  文件拷贝
//  fs.copyFile(src, dest[, flags], callback)
//  fs.copyFileSync(src, dest[, flags], callback)

//  异步拷贝
fs.copyFile('./test.json','test_copy_async.json',err=>{
    if(err) console.log(err);
})
// //  同步拷贝
try {
    fs.copyFileSync('./test.json','test_copy_sync.json')
} catch (error) {
    
}

//  文件删除
//  fs.unlink(path, callback)
//  fs.unlinkSync(path, callback)

//  异步删除
fs.unlink('./test_copy_async.json',err=>{
    if(err)console.log(err)
})
// //  同步删除
try {
    fs.unlinkSync('./test_copy_sync.json')
} catch (error) {
    console.log(error)
}

// 总结 :

// 同步都是在原api后面添加“Sync”,参数类似(path[, options], callback)
// 文件读取:readFile
// 文件写入:writeFile
// 文件追加:appendFile
// 文件拷贝:copyFile
// 文件删除:unlink

 文件夹相关api

//文件夹操作常用api
const fs = require('fs')

//  创建文件夹
//  fs.mkdir(path[, options], callback)
//  fs.mkdirSync(path[, options])

fs.mkdir('./static/js',{ recursive: true },err=>{
    //  recursive: true  创建/static/css目录,无论是否存在 /static 目录。
    if(err)console.log(err);
})

//  读取文件夹
fs.readdir('./static',{withFileTypes:false},(err,data)=>{
    if(err){
        console.log(err)
        return
    }
    console.log(data);
    // {withFileTypes:false},返回字符串组成的数组
    //  [ 'aaa.text', 'css', 'images', 'js' ]

    // {withFileTypes:true},返回dirent对象组成的数组
        // dirent.isFile() 判断是否是常规文件
        // dirent.isDirectory() 判断是否是文件目录
    // [ 
    //     Dirent { name: 'aaa.text', [Symbol(type)]: 1 },
    //     Dirent { name: 'css', [Symbol(type)]: 2 },
    //     Dirent { name: 'images', [Symbol(type)]: 2 },
    //     Dirent { name: 'js', [Symbol(type)]: 2 } 
    // ]
})

// 删除文件
// fs.rmdir(path, callback)
// fs.rmdirSync(path, callback)
// ps:添加options{recursive:true},报错
fs.rmdir('./static/images',err=>{
    if(err) console.log(err)
})

 

转载于:https://www.cnblogs.com/superjsman/p/11606974.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值