栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Node.js检查文件是否存在

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Node.js检查文件是否存在

为什么不尝试打开文件?

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);    }});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392105.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号