fs.exists
现在已被弃用,原因是我应该尝试打开文件并捕获错误,以确保在检查和打开之间无法删除文件。但如果我需要创建一个新文件而不是打开现有文件,我如何保证在尝试创建它之前不存在文件?
最佳答案
你不能。但是,您可以创建一个新文件或打开现有文件(如果存在):
fs.open("/path", "a+", function(err, data){ // open for reading and appending
if(err) return handleError(err);
// work with file here, if file does not exist it will be created
});
或者,使用“ax+”
打开它,如果它已经存在,则会出错,让您处理错误。
关于node.js - 如何保证文件在创建之前不存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32805388/