【问题标题】:Writing into a file only if the file content is different from the new data仅当文件内容与新数据不同时才写入文件
【发布时间】:2019-05-08 10:44:04
【问题描述】:

我想要这样的代码:

fs.writeFile(fullFileAddress , data, function (err)
{
    if(err)
    {
        if(err.code == 'ENOENT')
        {
            console.error( new Error(`#Space. Can not save file. message: ${err}`.red) );
        }
    }
    else
    {
        console.log("#eJS. %s created.", fullFileAddress);
    }
});

我必须先读取文件,然后将其与数据变量进行比较吗?如果不一样,那么我写入文件? 或者也许有更好的方法?

【问题讨论】:

  • 用相同的数据覆盖文件有关系吗?
  • 是的,当你写入一个文件时,你改变了 iNode 上的一些属性

标签: node.js linux file filesystems


【解决方案1】:

一个优雅的替代方法是将包含文件最后修改时间的stats.mtime 与数据分派时间进行比较,而不是将文件内容与数据进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2014-05-12
    • 2012-10-05
    • 2012-07-23
    相关资源
    最近更新 更多