node-复制文件
1 const path = require("path"); 2 const fs = require("fs"); 3 // 获取目录下的所有文件名 4 let fileNameList = fs.readdirSync(path.resolve(__dirname, "./template")) 5 console.log(fileNameList) 6 7 // 文件复制 8 // 1. 管道指引流的方式复制文件 9 let indexFile = fs.createReadStream(path.resolve(__dirname, "./template/index.js")); 10 let indexTo = fs.createWriteStream(path.resolve(process.cwd(), "./to/index.js")) 11 indexFile.pipe(indexTo) 12 13 // 2. 原生的readFile(Sync)和writeFile(Sync) ,参数为path和编码格式(或者内容) 14 // 区分同步和异步 15 let contentSync = fs.readFileSync(path.resolve(__dirname, "./template/render.vue"), "utf-8") 16 fs.writeFileSync(path.resolve(process.cwd(), "./to/render.vue"), contentSync) 17 fs.readFile(path.resolve(__dirname, "./template/render.vue"), "utf-8", function (err, data) { 18 console.log(err, data) 19 fs.writeFileSync(path.resolve(process.cwd(), "./to/render.vue"), data, function (error) { 20 console.log(error) 21 }) 22 }) 23 24 // 3. copyFile(formPath, toPath), 或者copyFileSync 25 copyFileSync(path.resolve(__dirname, "./template/render.vue"), path.resolve(__dirname, "./to/render.vue")) 26 fs.copyFile(path.resolve(__dirname, "./template/render.vue"), path.resolve(__dirname, "./to/render.vue"), 0, () => { 27 console.log("复制完成") 28 })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-05-08 手写横向滚动条与时间轴