温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/docs/runtime/file-io
点击访问原文链接

File I/O - Bun

File I/O - BunDocumentation Index

Fetch the complete documentation index at: /docs/llms.txt

Use this file to discover all available pages before exploring further.

Skip to main contentBun home pageSearch...⌘KInstall BunSearch...NavigationData & StorageFile I/ORuntimePackage ManagerBundlerTest RunnerGuidesReferenceBlogFeedback:first-child]:!hidden peer-[.is-custom]:[&>:first-child]:sm:!hidden peer-[.is-custom]:[&>:first-child]:md:!hidden peer-[.is-custom]:[&>:first-child]:lg:!hidden peer-[.is-custom]:[&>:first-child]:xl:!hidden">Get StartedWelcome to BunInstallationQuickstartTypeScriptTypeScript 6 and 7bun initbun createCore RuntimeBun RuntimeWatch ModeDebuggingREPLbunfig.tomlFile & Module SystemFile TypesModule ResolutionJSXAuto-installPluginsFile System RouterHTTP serverServerRoutingCookiesTLSError HandlingMetricsNetworkingFetchWebSocketsTCPUDPDNSData & StorageCookiesFile I/OStreamsBinary DataArchiveSQLSQLiteS3RedisConcurrencyWorkersProcess & SystemEnvironment VariablesShellSpawnWebViewCronInterop & ToolingNode-APIFFIC CompilerTranspilerUtilitiesCSRF ProtectionSecretsConsoleTOMLYAMLMarkdownJSON5JSONLHTMLRewriterImageHashingGlobSemverColorUtilsStandards & CompatibilityGlobalsBun APIsWeb APIsNode.js CompatibilityContributingRoadmapBenchmarkingContributingBuilding WindowsBindgenLicenseOn this pageReading files (Bun.file())Deleting files (file.delete())Writing files (Bun.write())Incremental writing with FileSinkDirectoriesReading directories (readdir)Reading directories recursivelyCreating directories (mkdir)BenchmarksReferenceData & StorageFile I/OCopy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_n4ctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">*]:[overflow-wrap:anywhere]">

Bun provides a set of optimized APIs for reading and writing files.

Copy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_1cctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">The Bun.file and Bun.write APIs documented on this page are heavily optimized and represent the recommended way to perform file-system tasks using Bun. For operations that are not yet available with Bun.file, such as mkdir or readdir, you can use Bun’s nearly complete implementation of the node:fs module. ​Reading files (Bun.file()) Bun.file(path): BunFile Create a BunFile instance with the Bun.file(path) function. A BunFile represents a lazily-loaded file; initializing it does not actually read the file from disk.
const foo = Bun.file("foo.txt"); // relative to cwd
foo.size; // number of bytes
foo.type; // MIME type
The reference conforms to the Blob interface, so the contents can be read in various formats.
const foo = Bun.file("foo.txt");

await foo.text(); // contents as a string
await foo.json(); // contents as a JSON object
await foo.stream(); // contents as ReadableStream
await foo.arrayBuffer(); // contents as ArrayBuffer
await foo.bytes(); // contents as Uint8Array
File references can also be created using numerical file descriptors or file:http:// URLs.
Bun.file(1234);
Bun.file(new URL(import.meta.url)); // reference to the current file
A BunFile can point to a location on disk where a file does not exist.
const notreal = Bun.file("notreal.txt");
notreal.size; // 0
notreal.type; // "text/plain;charset=utf-8"
const exists = await notreal.exists(); // false
The default MIME type is text/plain;charset=utf-8, but it can be overridden by passing a second argument to Bun.file.
const notreal = Bun.file("notreal.json", { type: "application/json" });
notreal.type; // => "application/json;charset=utf-8"
For convenience, Bun exposes stdin, stdout and stderr as instances of BunFile.
Bun.stdin; // readonly
Bun.stdout;
Bun.stderr;
​Deleting files (file.delete()) You can delete a file by calling the .delete() function.
await Bun.file("logs.json").delete();
​Writing files (Bun.write()) Bun.write(destination, data): Promise The Bun.write function is a multi-tool for writing payloads of all kinds to disk. The first argument is the destination which can have any of the following types: string: A path to a location on the file system. Use the "path" module to manipulate paths. URL: A file:http:// descriptor. BunFile: A file reference. The second argument is the data to be written. It can be any of the following: string Blob (including BunFile) ArrayBuffer or SharedArrayBuffer TypedArray (Uint8Array, et. al.) Response All possible permutations are handled using the fastest available system calls on the current platform.

See syscalls

OutputInputSystem callPlatformfilefilecopy_file_rangeLinuxfilepipesendfileLinuxpipepipespliceLinuxterminalfilesendfileLinuxterminalterminalsendfileLinuxsocketfile or pipesendfile (if http, not https)Linuxfile (doesn’t exist)file (path)clonefilemacOSfile (exists)filefcopyfilemacOSfileBlob or stringwritemacOSfileBlob or stringwriteLinux To write a string to disk:
const data = `It was the best of times, it was the worst of times.`;
await Bun.write("output.txt", data);
To copy a file to another location on disk:
const input = Bun.file("input.txt");
const output = Bun.file("output.txt"); // doesn't exist yet!
await Bun.write(output, input);
To write a byte array to disk:
const encoder = new TextEncoder();
const data = encoder.encode("datadatadata"); // Uint8Array
await Bun.write("output.txt", data);
To write a file to stdout:
const input = Bun.file("input.txt");
await Bun.write(Bun.stdout, input);
To write the body of an HTTP response to disk:
const response = await fetch("https://bun.com");
await Bun.write("index.html", response);
​Incremental writing with FileSink Bun provides a native incremental file writing API called FileSink. To retrieve a FileSink instance from a BunFile:
const file = Bun.file("output.txt");
const writer = file.writer();
To incrementally write to the file, call .write().
const file = Bun.file("output.txt");
const writer = file.writer();

writer.write("it was the best of times\n");
writer.write("it was the worst of times\n");
These chunks will be buffered internally. To flush the buffer to disk, use .flush(). This returns the number of flushed bytes.
writer.flush(); // write buffer to disk
The buffer will also auto-flush when the FileSink’s high water mark is reached; that is, when its internal buffer is full. This value can be configured.
const file = Bun.file("output.txt");
const writer = file.writer({ highWaterMark: 1024 * 1024 }); // 1MB
To flush the buffer and close the file:
writer.end();
Note that, by default, the bun process will stay alive until this FileSink is explicitly closed with .end(). To opt out of this behavior, you can “unref” the instance.
writer.unref();

// to "re-ref" it later
writer.ref();
​Directories Bun’s implementation of node:fs is fast. Use node:fs for working with directories in Bun. ​Reading directories (readdir) To read a directory in Bun, use readdir from node:fs.
import { readdir } from "node:fs/promises";

// read all the files in the current directory
const files = await readdir(import.meta.dir);
​Reading directories recursively To recursively read a directory in Bun, use readdir with recursive: true.
import { readdir } from "node:fs/promises";

// read all the files in the current directory, recursively
const files = await readdir("../", { recursive: true });
​Creating directories (mkdir) To recursively create a directory, use mkdir in node:fs:
import { mkdir } from "node:fs/promises";

await mkdir("path/to/dir", { recursive: true });
​Benchmarks The following is a 3-line implementation of the Linux cat command. cat.ts
// Usage
// bun ./cat.ts ./path-to-file

import { resolve } from "path";

const path = resolve(process.argv.at(-1));
await Bun.write(Bun.stdout, Bun.file(path));
To run the file: terminal
bun ./cat.ts ./path-to-file
It runs 2x faster than GNU cat for large files on Linux. ​Reference
interface Bun {
stdin: BunFile;
stdout: BunFile;
stderr: BunFile;

file(path: string | number | URL, options?: { type?: string }): BunFile;

write(
destination: string | number | BunFile | URL,
input: string | Blob | ArrayBuffer | SharedArrayBuffer | TypedArray | Response,
): Promisenumber>;
}

interface BunFile {
readonly size: number;
readonly type: string;

text(): Promisestring>;
stream(): ReadableStream;
arrayBuffer(): PromiseArrayBuffer>;
json(): Promiseany>;
writer(params: { highWaterMark?: number }): FileSink;
exists(): Promiseboolean>;
}

export interface FileSink {
write(chunk: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer): number;
flush(): number | Promisenumber>;
end(error?: Error): number | Promisenumber>;
start(options?: { highWaterMark?: number }): void;
ref(): void;
unref(): void;
}

Was this page helpful?

YesNoSuggest editsRaise issueCookiesPreviousStreamsNext⌘IxgithubdiscordyoutubePowered byThis documentation is built and hosted on Mintlify, a developer documentation platform

File I/O - Bun,AI智能索引,全网链接索引,智能导航,网页索引

    Bun provides a set of optimized APIs for reading and writing files.