node.js & fs & file read & file write All In One
node.js & fs & file read & file write All In One
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
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10827874.html
未经授权禁止转载,违者必究!