title: Creating an empty file of a certain size?
body: <p>How can we create an empty file of certain size? I have a requirement where I need to create an empty file (i.e. file filled with zero bytes). In order to do so, this is the approach I am currently taking:</p>
  
  <ol>
  <li>I create an empty file of zero byte size.</li>
  <li>Next I keep on appending zero bytes buffer (max 2 GB at a time) to that file till I reach the desired size.</li>
  </ol>
  
  <p>Here's the code I am using currently:</p>
  
  <pre><code>const createEmptyFileOfSize = (fileName, size) =&gt; {
    return new Promise((resolve, reject) =&gt; {
      try {
        //First create an empty file.
        fs.writeFile(fileName, Buffer.alloc(0), (error) =&gt; {
          if (error) {
            reject(error);
          } else {
            let sizeRemaining = size;
            do {
              const chunkSize = Math.min(sizeRemaining, buffer.kMaxLength);
              const dataBuffer = Buffer.alloc(chunkSize);
              try {
                fs.appendFileSync(fileName, dataBuffer);
                sizeRemaining -= chunkSize;
              } catch (error) {
                reject(error);
              }
            } while (sizeRemaining &gt; 0);
            resolve(true);
          }
        });
      } catch (error) {
        reject(error);
      }
    });
  };
  </code></pre>
  
  <p>While this code works and I am able to create very large files (though it takes significant time to create an empty large file [roughly about 5 seconds to create a 10 GB file]) however I am wondering if there's a better way of accomplishing this. </p>