The code below inserted somestring
to file but also will replace the text inside the file. how can fix this?
下面的代码将somestring插入到文件中,但也将替换文件中的文本。怎么解决这个问题?
fd = fs.openSync('file', 'r+')
buf = new Buffer('somestring')
fs.writeSync(fd, buf, 0, buf.length, 0)
fs.close(fd)
2 个解决方案
#1
10
Open the file in append mode using the a+
flag
使用a +标志以追加模式打开文件
var fd = fs.openSync('file', 'a+');
Or use a positional write
. To be able to append to end of file, use fs.appendFile
:
或者使用位置写入。要能够追加到文件末尾,请使用fs.appendFile:
fs.appendFile(fd, buf, err => {
//
});
Write to the beginning of a file:
写到文件的开头:
fs.write(fd, buf, 0, buf.length, 0);
EDIT:
I guess there isn't a single method call for that. But you can copy the contents of the file, write new data, and append the copied data.
我想没有一个方法调用。但您可以复制文件的内容,写入新数据并附加复制的数据。
var data = fs.readFileSync(file); //read existing contents into data
var fd = fs.openSync(file, 'w+');
var buffer = new Buffer('New text');
fs.writeSync(fd, buffer, 0, buffer.length, 0); //write new data
fs.writeSync(fd, data, 0, data.length, buffer.length); //append old data
// or fs.appendFile(fd, data);
fs.close(fd);
Please note that you must use the asynchronous versions of these methods if these operations aren't performed just once during initialization, as they'll block the event loop.
请注意,如果这些操作在初始化期间不执行一次,则必须使用这些方法的异步版本,因为它们将阻止事件循环。
#2
2
With small files you can do it like this:
使用小文件,您可以这样做:
let logPath = path.join(appPath, 'deploy.log');
let logRows = fs.readFileSync(logPath).toString().split('\n');
logRows.unshift('Your string here');
fs.writeFileSync(logPath, logRows.join('\n'));
Hope it would be useful for someone!
希望它对某人有用!
#1
10
Open the file in append mode using the a+
flag
使用a +标志以追加模式打开文件
var fd = fs.openSync('file', 'a+');
Or use a positional write
. To be able to append to end of file, use fs.appendFile
:
或者使用位置写入。要能够追加到文件末尾,请使用fs.appendFile:
fs.appendFile(fd, buf, err => {
//
});
Write to the beginning of a file:
写到文件的开头:
fs.write(fd, buf, 0, buf.length, 0);
EDIT:
I guess there isn't a single method call for that. But you can copy the contents of the file, write new data, and append the copied data.
我想没有一个方法调用。但您可以复制文件的内容,写入新数据并附加复制的数据。
var data = fs.readFileSync(file); //read existing contents into data
var fd = fs.openSync(file, 'w+');
var buffer = new Buffer('New text');
fs.writeSync(fd, buffer, 0, buffer.length, 0); //write new data
fs.writeSync(fd, data, 0, data.length, buffer.length); //append old data
// or fs.appendFile(fd, data);
fs.close(fd);
Please note that you must use the asynchronous versions of these methods if these operations aren't performed just once during initialization, as they'll block the event loop.
请注意,如果这些操作在初始化期间不执行一次,则必须使用这些方法的异步版本,因为它们将阻止事件循环。
#2
2
With small files you can do it like this:
使用小文件,您可以这样做:
let logPath = path.join(appPath, 'deploy.log');
let logRows = fs.readFileSync(logPath).toString().split('\n');
logRows.unshift('Your string here');
fs.writeFileSync(logPath, logRows.join('\n'));
Hope it would be useful for someone!
希望它对某人有用!