[英]How to import excel file using HTML Input file and read the file contents in Node.js (How to send the fullpath to Node.js)
[英]How do I read an input file, modify its contents and save the result to an output file with node.js?
在我的項目中,我有一個像這個程序中的輸入文件:
var fs = require('fs');
var str = fs.readFileSync('input.txt', 'utf8');
但我不得不在我的終端中使用這個命令:
cat input.txt | ./prog.sh > result.txt
你能幫我嗎,因為我沒有找到如何在終端中使用這種命令在 JS 中編寫通用輸入文件?
以下代碼允許在終端命令的入口處獲取文檔的每種名稱類型,此代碼運行良好:
#!/usr/bin/env 節點
let chunk = "";
process.stdin.on("data", data => {
chunk += data.toString();
});
process.stdin.on("end", () => {
chunk.replace(/^\s*[\r\n]/gm,"").split(/\s+/).forEach(function (s) {
process.stdout.write(
s === 'bob'
? 'boy \n'
: s === 'alicia'
? 'girl\n'
: s === 'cookie'
? 'dog \n'
: 'unknown \n');
});
});
我們可以使用以下命令正確執行它: cat input.txt | /prog2.js > result.txt
cat input.txt | /prog2.js > result.txt
這應該完全覆蓋 result.txt 文件,或者如果它不存在則重新創建它。
const fs = require('fs');
const inputFile = "./input.txt";
const outputFile = "./result.txt";
var str = fs.readFileSync('input.txt', 'utf8').toString();
var prog = (fileStr) => {
let modifiedStr = fileStr.trim() + " !!!modified!!!"; // do something to modify the string
return modifiedStr;
};
let fileContents = prog(str);
fs.writeFile(outputFile, fileContents, function(err){
if (err) throw err;
console.log('string saved to ' + outputFile);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.