在Node.js中创建一个空文件? [英] Create an empty file in Node.js?

查看:180
本文介绍了在Node.js中创建一个空文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我用

$ $ $ $ $ $ c $ f $ c>

但这有点棘手。在Node.js中创建一个空文件是否有标准方法?

解决方案

如果你想强制文件是空的,那么你想用'w'标志来代替:

  var fd = fs.openSync(filepath,'w'); 

如果文件存在,会截断文件,如果不存在则创建文件。



如果您不需要它返回的文件描述符,将它包装在 fs.closeSync 调用中。

  fs.closeSync(fs.openSync(filepath,'w')); 


For now I use

fs.openSync(filepath, 'a')

But it's a little tricky. Is there a 'standard' way to create an empty file in Node.js?

解决方案

If you want to force the file to be empty then you want to use the 'w' flag instead:

var fd = fs.openSync(filepath, 'w');

That will truncate the file if it exists and create it if it doesn't.

Wrap it in an fs.closeSync call if you don't need the file descriptor it returns.

fs.closeSync(fs.openSync(filepath, 'w'));

这篇关于在Node.js中创建一个空文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆