seek
seek
方法根据 whence
指定的模式,将文件位置指示器设置为传递的 offset
字节。调用会返回资源内的新位置(从开头算起的字节数)。
根据传递的 SeekMode,偏移量解释如下:
- 使用
SeekMode.Start
时,偏移量必须大于或等于零。 - 使用
SeekMode.Current
时,偏移量可以是正数或负数。 - 使用
SeekMode.End
时,偏移量必须小于或等于零。
参数
参数 | 类型 | 描述 |
---|---|---|
offset | number | 从 whence 指定位置开始的字节偏移量。 |
whence | SeekMode | 应用偏移量的起始位置。 |
返回值
一个 Promise,解析为文件内的新偏移量。
示例
import { open, SeekMode } from 'k6/experimental/fs';
const file = await open('bonjour.txt');
export default async function () {
// Seek 6 bytes from the start of the file
await file.seek(6, SeekMode.Start);
// Seek 2 more bytes from the current position
await file.seek(2, SeekMode.Current);
// Seek backwards 2 bytes from the end of the file
await file.seek(-2, SeekMode.End);
}