为什么不尝试打开文件?
fs.open('YourFile', 'a', function (err, fd) { ... })无论如何,经过一分钟的搜索,请尝试以下操作:
var path = require('path');path.exists('foo.txt', function(exists) { if (exists) { // do something } });// orif (path.existsSync('foo.txt')) { // do something }对于Node.js v0.12.x及更高版本
双方path.exists
并fs.exists
已弃用
*编辑:
已更改:
else if(err.pre == 'ENOENT')
至:
else if(err.pre === 'ENOENT')
林特抱怨双重等于不是三次等于。
使用fs.stat:
fs.stat('foo.txt', function(err, stat) { if(err == null) { console.log('File exists'); } else if(err.pre === 'ENOENT') { // file does not exist fs.writeFile('log.txt', 'Some logn'); } else { console.log('Some other error: ', err.pre); }});


