const fs = require('fs');
// Read the file
fs.readFile('settings.ini', 'utf8', function(err, data) {
if (err) {
console.error(err);
return;
}
// Parse the data (assuming it's in the format "key=value")
const settings = {};
data.split('\n').forEach(line => {
const [key, value] = line.split('=');
settings[key] = value;
});
// Now `settings` is an object with the data from the file
// You can modify it as needed, for example:
settings.siteName = 'New site name';
// Then you can write it back to the file
const newData = Object.entries(settings).map(([key, value]) => `${key}=${value}`).join('\n');
fs.writeFile('settings.ini', newData, 'utf8', function(err) {
if (err) {
console.error(err);
return;
}
console.log('File has been updated');
});
});
1条答案
按热度按时间zfycwa2u1#
您可以使用Node.js中内置的fs(文件系统)模块来读取和写入文件。我留下了一个基本的例子,告诉你如何做到这一点。