filehandle.truncate(len)
-
len<integer> 默认值:0¥
len<integer> Default:0 -
返回:<Promise> 成功时将使用
undefined履行。¥Returns: <Promise> Fulfills with
undefinedupon success.
截断文件。
¥Truncates the file.
如果文件大于 len 个字节,则仅前 len 个字节将保留在文件中。
¥If the file was larger than len bytes, only the first len bytes will be
retained in the file.
下面的示例仅保留文件的前四个字节:
¥The following example retains only the first four bytes of the file:
import { open } from 'node:fs/promises';
let filehandle = null;
try {
filehandle = await open('temp.txt', 'r+');
await filehandle.truncate(4);
} finally {
await filehandle?.close();
} 如果文件先前小于 len 个字节,则将其扩展,并且扩展部分将使用空字节('\0')填充:
¥If the file previously was shorter than len bytes, it is extended, and the
extended part is filled with null bytes ('\0'):
如果 len 为负数,则将使用 0。
¥If len is negative then 0 will be used.