-1
各行が一定の条件を満たしている場合は、ファイルを1行ずつ読み込み、読み込み中に改行を同じファイルに書き込む必要があります。何が最善の方法かもしれない。ファイルを1行ずつ読み込み、それに基づいて同じファイルに改行を書き込む - nodejs
各行が一定の条件を満たしている場合は、ファイルを1行ずつ読み込み、読み込み中に改行を同じファイルに書き込む必要があります。何が最善の方法かもしれない。ファイルを1行ずつ読み込み、それに基づいて同じファイルに改行を書き込む - nodejs
function (file, callback) {
fs.readFile(file, (err, 'utf8', data) => {
if (err) return callback(err);
var lines = data.split('\n');
fs.open(file, 'w', (err, fd) => {
if (err) return callback(err)
lines.forEach(line => {
if (line === 'meet your condition') {
// do your write using fs.write(fd,)
}
})
callback();
})
})
}
fsの助けを借りてノードfsモジュールを使用すると、非同期的にも同期的にも操作を実行できます。以下非同期
function readWriteData(savPath, srcPath) {
fs.readFile(srcPath, 'utf8', function (err, data) {
if (err) throw err;
//Do your processing, MD5, send a satellite to the moon or can add conditions , etc.
fs.writeFile (savPath, data, function(err) {
if (err) throw err;
console.log('complete');
});
});
}
に同期の例の一例として、ある
function readFileContent(srcPath, callback) {
fs.readFile(srcPath, 'utf8', function (err, data) {
if (err) throw err;
callback(data);
}
);
}
function writeFileContent(savPath, srcPath) {
readFileContent(srcPath, function(data) {
fs.writeFile (savPath, data, function(err) {
if (err) throw err;
console.log('complete');
});
});
}