我想用node.js删除一个简单文本文件的内容。
或者用新的/空的文件替换该文件。
我怎样才能在节点中实现这一点?
您正在寻找fs.truncate http://nodejs.org/api/fs.html#fs_fs_truncate_path_len_callback or fs.writeFile https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
以下任一方法都可以:
const fs = require('fs')
fs.truncate('/path/to/file', 0, function(){console.log('done')})
or
const fs = require('fs')
fs.writeFile('/path/to/file', '', function(){console.log('done')})
还有同步版本 http://nodejs.org/api/fs.html#fs_fs_truncatesync_path_len of 两种功能 https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options这在某些情况下很有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)