当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Node.js fs.truncate(path[, len], callback)用法及代码示例

fs.truncate(path[, len], callback)

历史
版本变化
v18.0.0

将无效回调传递给 callback 参数现在会抛出 ERR_INVALID_ARG_TYPE 而不是 ERR_INVALID_CALLBACK

v16.0.0

如果返回多个错误,则返回的错误可能是 AggregateError

v10.0.0

callback 参数不再是可选的。不通过它将在运行时抛出TypeError

v7.0.0

callback 参数不再是可选的。不通过它将发出带有 ID DEP0013 的弃用警告。

v0.8.6

添加于:v0.8.6


参数

截断文件。除了可能的异常之外,没有为完成回调提供任何参数。文件说明符也可以作为第一个参数传递。在这种情况下,调用fs.ftruncate()

import { truncate } from 'node:fs';
// Assuming that 'path/file.txt' is a regular file.
truncate('path/file.txt', (err) => {
  if (err) throw err;
  console.log('path/file.txt was truncated');
});const { truncate } = require('node:fs');
// Assuming that 'path/file.txt' is a regular file.
truncate('path/file.txt', (err) => {
  if (err) throw err;
  console.log('path/file.txt was truncated');
});

不推荐传递文件说明符,并且可能会导致将来抛出错误。

有关更多详细信息,请参阅 POSIX truncate(2) 文档。

相关用法


注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 fs.truncate(path[, len], callback)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。