node.js & fs & file read & file write
node.js & fs & file read & file write
https://nodejs.org/api/fs.html
const fs = require("fs");
// absolute path
fs.open("/open/some/file.txt", "r", (err, fd) => {
if (err) {
throw err;
}
fs.close(fd, (err) => {
if (err) {
throw err;
}
});
});
// relative path
fs.open("file.txt", "r", (err, fd) => {
if (err) {
throw err;
}
fs.close(fd, (err) => {
if (err) {
throw err;
}
});
});
readline
https://nodejs.org/api/readline.html
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();
});

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 path
fs.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
https://stackoverflow.com/a/15554600/5934465


write & read
https://stackoverflow.com/a/15554600/5934465
https://stackoverflow.com/a/2497040/5934465

// preview.js
const 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.js
const 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.js
export const APP_ENV = "testing";
global var
https://stackabuse.com/using-global-variables-in-node-js

node.js & fs & file read & file write的更多相关文章
- 从官网学习Node.js FS模块方法速查
最新文档请查看仓库 https://github.com/wangduandu... 1. File System 所有文件操作提供同步和异步的两种方式,本笔记只记录异步的API 异步方式其最后一个参 ...
- Node.js FS模块方法速查
1. File System 所有文件操作提供同步和异步的两种方式,本笔记只记录异步的API 异步方式其最后一个参数是回调函数.回调函数的第一个参数往往是错误对象,如果没有发生参数,那么第一个参数可能 ...
- 极简 Node.js 入门 - 3.1 File System API 风格
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- node.js fs.open 和 fs.write 读取文件和改写文件
Node.js的文件系统的Api //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile( ...
- [node.js] fs.renameSync()报错
初学node.js,跟着node入门,操作了一遍.在最后一步,上传图片并显示时遇到报错 fs.js: throw err; ^ Error: ENOENT: no such file or direc ...
- node.js fs,http
error: EventEmitter定义了一个特殊的时间error,它包含了‘错误’的语义,当error被发射时,EventEmitter规定如果没有 响应的监听器,Node.js会把它当做异常,退 ...
- Node.js——fs模块(文件系统),创建、删除目录(文件),读取写入文件流
/* 1. fs.stat 检测是文件还是目录(目录 文件是否存在) 2. fs.mkdir 创建目录 (创建之前先判断是否存在) 3. fs.writeFile 写入文件(文件不存在就创建,但不能创 ...
- node.js fs、http使用
学习node核心模块http.fs;的使用 首先在server.js文件中require两个模块http.fs; let fs = require('fs')let http = require (' ...
- Node.js fs文件系统模块
一.读取文件几个步骤 1.先引入fs文件系统模块 2.使用readfile 或 readFileSync 注意点:它们的回调函数有两个参数,第一个是err,第二个是data,得到的data是buffe ...
随机推荐
- Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79530732
Linux下nf_conntrack(最全面)_董明磊-CSDN博客_nf_conntrack https://blog.csdn.net/qq_35299863/article/details/79 ...
- get uuid
https://wx2.qq.com/?&lang=zh_CN /** * 启动二维码登录 */ function doQrcodeLogin() { loginFactory.getUUID ...
- circus reload
circus reload Configuration - Circus 0.15.0 documentation https://circus.readthedocs.io/en/latest/fo ...
- 【c++小知识】static用法浅析
一.前言 C++的关键字static分两种用法,在面向过程程序设计(c语言中的普通变量和函数)中的使用和在面向对象程序设计(c++中的类)中的使用 二.面向过程程序设计中的static(静态变量.静态 ...
- Redis分布式锁升级版RedLock及SpringBoot实现
分布式锁概览 在多线程的环境下,为了保证一个代码块在同一时间只能由一个线程访问,Java中我们一般可以使用synchronized语法和ReetrantLock去保证,这实际上是本地锁的方式.但是现在 ...
- java小技巧
String 转 Date String classCode = RequestHandler.getString(request, "classCode"); SimpleDat ...
- react报错 TypeError: Cannot read property 'setState' of undefined
代码如下: class test extends Component { constructor(props) { super(props); this.state = { liked: false ...
- Language Guide (proto3) | proto3 语言指南(九)Oneof结构
Oneof - Oneof结构 如果消息包含多个字段,并且最多只能同时设置一个字段,则可以使用oneof功能强制执行此行为并节省内存. oneof字段与常规字段类似,但oneof共享内存中的所有字段除 ...
- 利用Javascript制作网页特效(时间特效)
在网页中经常可以看到各种各样的动态时间显示,在网页中合理地使用时间可以增加网页的时效感. 显示当前时间 getHours().getMinutes().getSeconds()分别获得当前小时数.当前 ...
- Python 学习博客地址
Alex https://www.cnblogs.com/alex3714林海峰 https://www.cnblogs.com/linhaifeng武佩奇 https://www.cnblogs. ...