博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node.js & fs & file read & file write
阅读量:5281 次
发布时间:2019-06-14

本文共 3625 字,大约阅读时间需要 12 分钟。

node.js & fs & file read & file write

const fs = require("fs");// absolute pathfs.open("/open/some/file.txt", "r", (err, fd) => {    if (err) {        throw err;    }    fs.close(fd, (err) => {        if (err) {            throw err;        }    });});// relative pathfs.open("file.txt", "r", (err, fd) => {    if (err) {        throw err;    }    fs.close(fd, (err) => {        if (err) {            throw err;        }    });});

readline

const readline = require('readline');const rl = readline.createInterface({    input: process.stdin,    output: process.stdout});rl.question('What do you think of Node.js? ', (answer) => {  // TODO: Log the answer in a database  console.log(`Thank you for your valuable feedback: ${answer}`);  rl.close();});

740516-20190507204431822-2143075547.png

const fs = require("fs");const readline = require('readline');const rl = readline.createInterface({    input: process.stdin,    output: process.stdout});// absolute path// fs.open("/open/some/file.txt", "r", (err, fd) => {//     if (err) {//         throw err;//     }//     fs.close(fd, (err) => {//         if (err) {//             throw err;//         }//     });// });// relative pathfs.open("file.txt", "r", (err, fd) => {    if (err) {        throw err;    }    console.log(`fd =`, fd);// 3    rl.on('line', (fd) => {    console.log(`Received: ${fd}`);    });    fs.close(fd, (err) => {        if (err) {            throw err;        }    });});

bug

740516-20190507205152685-782143968.png

740516-20190507205214571-1458909914.png


write & read

740516-20190507210440369-1978293953.png

// preview.jsconst fs = require("fs");const readline = require("readline");fs.writeFile("file.js", `export const APP_ENV = "preview";`, function(err) {    if(err) {        console.log(err);        return err;    }    let env = "preview";    console.log("The file was saved!", env);});let rd = readline.createInterface({    input: fs.createReadStream("file.js"),    output: process.stdout,    console: false});rd.on("line", function(line) {    console.log(`line =`, line);});// fs.writeFile("file.txt", "prview", function(err) {//     if(err) {//         console.log(err);//         return err;//     }//     console.log("The file was saved!");// });// global.APP_ENV = "testing";// let APP_ENV = global.APP_ENV;// console.log(`APP_ENV =`, APP_ENV);// let rd = readline.createInterface({//     input: fs.createReadStream('file.txt'),//     output: process.stdout,//     console: false// });// rd.on('line', function(line) {//     console.log(`line =`, line);// });
// testing.jsconst fs = require("fs");const readline = require("readline");fs.writeFile("file.js", `export const APP_ENV = "testing";`, function(err) {    if(err) {        console.log(err);        return err;    }    let env = "testing";    console.log("The file was saved!", env);});// const rl = readline.createInterface({//     input: process.stdin,//     output: process.stdout// });// absolute path// fs.open("/open/some/file.txt", "r", (err, fd) => {//     if (err) {//         throw err;//     }//     fs.close(fd, (err) => {//         if (err) {//             throw err;//         }//     });// });// global.APP_ENV = "testing";// let APP_ENV = global.APP_ENV;// console.log(`APP_ENV =`, APP_ENV);// relative path// fs.open("file.js", "r", (err, fd) => {//     if (err) {//         throw err;//     }//     console.log(`fd =`, fd);// 3//     fs.close(fd, (err) => {//         if (err) {//             throw err;//         }//     });// });let rd = readline.createInterface({    input: fs.createReadStream("file.js"),    output: process.stdout,    console: false});rd.on("line", function(line) {    console.log(`line =`, line);});
// file.jsexport const APP_ENV = "testing";

global var

740516-20190507205344209-1811914145.png


转载于:https://www.cnblogs.com/xgqfrms/p/10827874.html

你可能感兴趣的文章
Android异步加载网络图片
查看>>
python学习之 - XML
查看>>
css问题小计
查看>>
Laravel学习笔记(三)数据库 数据库迁移
查看>>
ORACLE查看并修改最大连接数
查看>>
box-flex不均分问题
查看>>
Python--GIL 详解
查看>>
Oracle数据导入Mysql中
查看>>
BZOJ-4424 &&CodeForces-19E Fairy DP+dfs (Link-Cut-Tree可A)
查看>>
MongoDB学习笔记——聚合操作之group,distinct,count
查看>>
大道至简读后感(第四章)
查看>>
IDA IDC Tutorials: Additional Auto-Commenting
查看>>
k8s-存储卷1-十二
查看>>
Android 自定义ViewGroup实现弧形菜单
查看>>
ABP框架系列之四十一:(Nuget-Packages-Nuget包)
查看>>
head first 设计模式文摘
查看>>
在Android中Intent的概念及应用(二)——Intent过滤器相关选项
查看>>
数据库备份问题
查看>>
11.Java2核心技术—Java5.0新特性
查看>>
前端面试题(4)iframe有哪些优点?iframe缺点是什么?
查看>>