fs.truncate(path[, len], callback)
path<string> | <Buffer> | <URL>len<integer> 默认值:0callback<Function>err<Error> | <AggregateError>
截断文件。
除了可能的异常之外,没有为完成回调提供任何参数。
文件描述符也可以作为第一个参数传入。
在这种情况下,fs.ftruncate() 被调用。
import { truncate } from 'node:fs';
// 假设 'path/file.txt' 是普通文件。
truncate('path/file.txt', (err) => {
if (err) throw err;
console.log('path/file.txt was truncated');
});const { truncate } = require('node:fs');
// 假设 'path/file.txt' 是普通文件。
truncate('path/file.txt', (err) => {
if (err) throw err;
console.log('path/file.txt was truncated');
});不推荐传入文件描述符,并且可能会导致将来抛出错误。
有关更多详细信息,请参阅 POSIX truncate(2) 文档。
path<string> | <Buffer> | <URL>len<integer> Default:0callback<Function>err<Error> | <AggregateError>
Truncates the file. No arguments other than a possible exception are
given to the completion callback. A file descriptor can also be passed as the
first argument. In this case, fs.ftruncate() is called.
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');
});Passing a file descriptor is deprecated and may result in an error being thrown in the future.
See the POSIX truncate(2) documentation for more details.