filehandle.truncate(len)
截断文件。
如果文件大于 len
个字节,则仅前 len
个字节将保留在文件中。
下面的示例仅保留文件的前四个字节:
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'
)填充:
如果 len
为负数,则将使用 0
。
Truncates the file.
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();
}
If the file previously was shorter than len
bytes, it is extended, and the
extended part is filled with null bytes ('\0'
):
If len
is negative then 0
will be used.