import {appendFile, readFile} from 'fs'
// Read data from an Apache server's access log
readFile(
'/var/log/apache2/access_log',
{encoding: 'utf8'},
(error, data) => {
if (error) {
console.error('error reading!', error)
return
}
console.info('success reading!', data)
}
)
// Concurrently, write data to the same access log
appendFile(
'/var/log/apache2/access_log',
'New access log entry',
error => {
if (error) {
console.error('error writing!', error)
}
})
是否可以确保在appendFile
写入文件系统之前完成读取,还是可以在readFile
完成之前附加我的数据,以便readFile
返回我新添加的数据?
答案 0 :(得分:1)
通过快速测试进行尝试,确实很不稳定:
https://gist.github.com/bcherny/029473f21833a73126d2e1dce53f2a6a