此代碼片段將字符串寫入磁盤上的特定 絕對路徑。
它使用快速的 Bun.write() API 有效地將數據寫入磁盤。第一個參數是 目標;第二個是要寫入的 數據。
ts
const path = "/path/to/file.txt";
await Bun.write(path, "Lorem ipsum");任何相對路徑都將相對於項目根目錄(包含 package.json 文件的最近目錄)進行解析。
ts
const path = "./file.txt";
await Bun.write(path, "Lorem ipsum");你可以傳遞 BunFile 作為目標。Bun.write() 會將數據寫入其關聯的路徑。
ts
const path = Bun.file("./file.txt");
await Bun.write(path, "Lorem ipsum");Bun.write() 返回寫入磁盤的字節數。
ts
const path = "./file.txt";
const bytes = await Bun.write(path, "Lorem ipsum");
// => 11請參閱 文檔 > API > File I/O 獲取 Bun.write() 的完整文檔。