appendfile

Replace a line in txt file using JavaScript

萝らか妹 提交于 2020-06-13 00:35:52
问题 I am trying to simply replace a line in a text file using JavaScript. The idea is: var oldLine = 'This is the old line'; var newLine = 'This new line replaces the old line'; Now i want to specify a file, find the oldLine and replace it with the newLine and save it. Anyone who can help me here? 回答1: This should do it var fs = require('fs') fs.readFile(someFile, 'utf8', function (err,data) { var formatted = data.replace(/This is the old line/g, 'This new line replaces the old line'); fs

Replace a line in txt file using JavaScript

耗尽温柔 提交于 2020-06-13 00:35:13
问题 I am trying to simply replace a line in a text file using JavaScript. The idea is: var oldLine = 'This is the old line'; var newLine = 'This new line replaces the old line'; Now i want to specify a file, find the oldLine and replace it with the newLine and save it. Anyone who can help me here? 回答1: This should do it var fs = require('fs') fs.readFile(someFile, 'utf8', function (err,data) { var formatted = data.replace(/This is the old line/g, 'This new line replaces the old line'); fs

Replace a line in txt file using JavaScript

匆匆过客 提交于 2020-06-13 00:35:12
问题 I am trying to simply replace a line in a text file using JavaScript. The idea is: var oldLine = 'This is the old line'; var newLine = 'This new line replaces the old line'; Now i want to specify a file, find the oldLine and replace it with the newLine and save it. Anyone who can help me here? 回答1: This should do it var fs = require('fs') fs.readFile(someFile, 'utf8', function (err,data) { var formatted = data.replace(/This is the old line/g, 'This new line replaces the old line'); fs

NodeJS笔记: 文件操作大全

断了今生、忘了曾经 提交于 2019-12-18 18:27:03
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 文件I/O fs模块的基本用法 开发中我们经常会有文件I/O的需求,node.js中提供一个名为fs的模块来支持I/O操作,fs模块的文件I/O是对标准POSIX函数的简单封装。 writeFile函数的基本用法 文件I/O,写入是必修课之一。fs模块提供writeFile函数,可以异步的将数据写入一个文件, 如果文件已经存在则会被替换。用法如下: 例: fs.writeFile(filename, data, callback) var fs= require("fs"); fs.writeFile('test.txt', 'Hello Node', function (err) { if (err) throw err; console.log('Saved successfully'); //文件被保存 }); appendFile函数的基本用法 writeFile函数虽然可以写入文件,但是如果文件已经存在,我们只是想添加一部分内容,它就不能满足我们的需求了,很幸运,fs模块中还有appendFile函数,它可以将新的内容追加到已有的文件中,如果文件不存在,则会创建一个新的文件。使用方法如下: 例: fs.appendFile(文件名,数据,编码,回调函数(err)); var fs= require(

Append one file into another file

社会主义新天地 提交于 2019-12-13 09:40:13
问题 I've been working on this for a bit and all the examples I've found have been to append a given string into the file, but I haven't been able to find any luck with appending a whole file at the end of a file. Everything's been using .appendAllText or appendText which doesn't suit my needs. My files are .sgm . In my code I first grab all the sgm files, then I check if that file ends with _Ch#. I'm ready to append the file to the master, but so far all I've been able to do is append the string

Append data to file with firefox extension

我与影子孤独终老i 提交于 2019-12-11 02:13:44
问题 I'm trying to build a firefox extension and i want to write things periodically in a file. So i want a file in which i append new strings. The following code writes the file but at the end the file contains only the last string i wrote and not the previous. Can you help me? mydir=null; mylog=null; mystream=null; function initFolder() { var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties); mydir = dirSvc.get("ProfD",