<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>File on Grafana Labs</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/</link><description>Recent content in File on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/index.xml" rel="self" type="application/rss+xml"/><item><title>read</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/read/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/read/</guid><content><![CDATA[&lt;h1 id=&#34;read&#34;&gt;read&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;read&lt;/code&gt; method is used to read a chunk of the file into an &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Uint8Array&lt;/a&gt; buffer.&lt;/p&gt;
&lt;p&gt;It resolves to either the number of bytes read during the operation, or &lt;code&gt;null&lt;/code&gt; if there was nothing more to read.&lt;/p&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Parameter&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Type&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;buffer&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;&lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Uint8Array&lt;/a&gt;&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;The buffer to read the data into.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;returns&#34;&gt;Returns&lt;/h2&gt;
&lt;p&gt;A &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Promise&lt;/a&gt; resolving to the number of bytes read or &lt;code&gt;null&lt;/code&gt; if the end of the file has been reached.&lt;/p&gt;
&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;
&lt;h3 id=&#34;reading-a-file&#34;&gt;Reading a file&lt;/h3&gt;
&lt;p&gt;In the following example, we open a file and read it in chunks of 128 bytes until we reach the end of the file.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { open, SeekMode } from &amp;#39;k6/experimental/fs&amp;#39;;

const file = await open(&amp;#39;bonjour.txt&amp;#39;);

export default async function () {
  // Seek to the beginning of the file
  await file.seek(0, SeekMode.Start);

  const buffer = new Uint8Array(128);

  let totalBytesRead = 0;
  while (true) {
    // Read into the buffer
    const bytesRead = await file.read(buffer);
    if (bytesRead == null) {
      // EOF
      break;
    }

    // Do something useful with the content of the buffer
    totalBytesRead &amp;#43;= bytesRead;

    // If bytesRead is less than the buffer size, we&amp;#39;ve read the whole file
    if (bytesRead &amp;lt; buffer.byteLength) {
      break;
    }
  }
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3 id=&#34;readall-helper-function&#34;&gt;&lt;code&gt;readAll&lt;/code&gt; helper function&lt;/h3&gt;
&lt;p&gt;The following helper function can be used to read the entire contents of a file into a &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Uint8Array&lt;/a&gt; buffer.&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { open, SeekMode } from &amp;#39;k6/experimental/fs&amp;#39;;

let file;
(async function () {
  file = await open(&amp;#39;bonjour.txt&amp;#39;);
})();

async function readAll(file) {
  // Seek to the beginning of the file
  await file.seek(0, SeekMode.Start);

  const fileInfo = await file.stat();
  const buffer = new Uint8Array(fileInfo.size);

  const bytesRead = await file.read(buffer);
  if (bytesRead !== fileInfo.size) {
    throw new Error(
      &amp;#39;unexpected number of bytes read; expected &amp;#39; &amp;#43;
        fileInfo.size &amp;#43;
        &amp;#39; but got &amp;#39; &amp;#43;
        bytesRead &amp;#43;
        &amp;#39; bytes&amp;#39;
    );
  }

  return buffer;
}

export default async function () {
  // Read the whole file
  const fileContent = await readAll(file);
  console.log(JSON.stringify(fileContent));
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="read">read&lt;/h1>
&lt;p>The &lt;code>read&lt;/code> method is used to read a chunk of the file into an &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" target="_blank" rel="noopener noreferrer">Uint8Array&lt;/a> buffer.&lt;/p>
&lt;p>It resolves to either the number of bytes read during the operation, or &lt;code>null&lt;/code> if there was nothing more to read.&lt;/p></description></item><item><title>seek</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/seek/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/seek/</guid><content><![CDATA[&lt;h1 id=&#34;seek&#34;&gt;seek&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;seek&lt;/code&gt; method sets the file position indicator for the file to the passed &lt;code&gt;offset&lt;/code&gt; bytes, under the mode given by &lt;code&gt;whence&lt;/code&gt;. The call resolves to the new position within the resource (bytes from the start).&lt;/p&gt;
&lt;p&gt;Based on the 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/seekmode/&#34;&gt;SeekMode&lt;/a&gt; passed, the offset is interpreted as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;when using &lt;code&gt;SeekMode.Start&lt;/code&gt;, the offset must be greater than or equal to zero.&lt;/li&gt;
&lt;li&gt;when using &lt;code&gt;SeekMode.Current&lt;/code&gt;, the offset can be positive or negative.&lt;/li&gt;
&lt;li&gt;when using &lt;code&gt;SeekMode.End&lt;/code&gt;, the offset must be less than or equal to zero.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;section class=&#34;expand-table-wrapper&#34;&gt;&lt;div class=&#34;button-div&#34;&gt;
      &lt;button class=&#34;expand-table-btn&#34;&gt;Expand table&lt;/button&gt;
    &lt;/div&gt;&lt;div class=&#34;responsive-table-wrapper&#34;&gt;
    &lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Parameter&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Type&lt;/th&gt;
              &lt;th style=&#34;text-align: left&#34;&gt;Description&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;offset&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;number&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;The offset in bytes from the position specified by &lt;code&gt;whence&lt;/code&gt;.&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;whence&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/seekmode/&#34;&gt;SeekMode&lt;/a&gt;&lt;/td&gt;
              &lt;td style=&#34;text-align: left&#34;&gt;The position from which the offset is applied.&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/section&gt;&lt;h2 id=&#34;returns&#34;&gt;Returns&lt;/h2&gt;
&lt;p&gt;A &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Promise&lt;/a&gt; resolving to the new offset within the file.&lt;/p&gt;
&lt;h2 id=&#34;example&#34;&gt;Example&lt;/h2&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { open, SeekMode } from &amp;#39;k6/experimental/fs&amp;#39;;

const file = await open(&amp;#39;bonjour.txt&amp;#39;);

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);
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="seek">seek&lt;/h1>
&lt;p>The &lt;code>seek&lt;/code> method sets the file position indicator for the file to the passed &lt;code>offset&lt;/code> bytes, under the mode given by &lt;code>whence&lt;/code>. The call resolves to the new position within the resource (bytes from the start).&lt;/p></description></item><item><title>stat</title><link>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/stat/</link><pubDate>Mon, 21 Sep 2026 15:16:28 +0000</pubDate><guid>https://grafana.com/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/file/stat/</guid><content><![CDATA[&lt;h1 id=&#34;stat&#34;&gt;stat&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;stat&lt;/code&gt; method returns a promise resolving to a 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/fileinfo/&#34;&gt;FileInfo&lt;/a&gt; object with information about the file.&lt;/p&gt;
&lt;h2 id=&#34;returns&#34;&gt;Returns&lt;/h2&gt;
&lt;p&gt;A &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Promise&lt;/a&gt; resolving to a 
    &lt;a href=&#34;/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/fileinfo/&#34;&gt;FileInfo&lt;/a&gt; object with information about the file.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;JavaScript&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-javascript&#34;&gt;import { open, SeekMode } from &amp;#39;k6/experimental/fs&amp;#39;;

const file = await open(&amp;#39;bonjour.txt&amp;#39;);

export default async function () {
  // About information about the file
  const fileinfo = await file.stat();
  if (fileinfo.name != &amp;#39;bonjour.txt&amp;#39;) {
    throw new Error(&amp;#39;Unexpected file name&amp;#39;);
  }

  console.log(JSON.stringify(fileinfo));
}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
]]></content><description>&lt;h1 id="stat">stat&lt;/h1>
&lt;p>The &lt;code>stat&lt;/code> method returns a promise resolving to a
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/fileinfo/">FileInfo&lt;/a> object with information about the file.&lt;/p>
&lt;h2 id="returns">Returns&lt;/h2>
&lt;p>A &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" target="_blank" rel="noopener noreferrer">Promise&lt;/a> resolving to a
&lt;a href="/docs/k6/v2.3.x/javascript-api/k6-experimental/fs/fileinfo/">FileInfo&lt;/a> object with information about the file.&lt;/p></description></item></channel></rss>