@tauri-apps/plugin-fs
存取檔案系統。
安全性
此模組防止路徑遍歷,不允許使用父目錄存取器(即,不允許使用「/usr/path/to/../file」或「../path/to/file」路徑)。使用此 API 存取的路徑必須相對於基本目錄之一,或使用 path API 建立。
API 具有作用域配置,強制您使用 glob 模式限制可以存取的路徑。
作用域配置是一個 glob 模式陣列,描述允許的檔案/目錄路徑。例如,此作用域配置允許所有已啟用的 fs
API(僅)存取 $APPDATA
目錄的 databases 目錄中的檔案
{ "permissions": [ { "identifier": "fs:scope", "allow": [{ "path": "$APPDATA/databases/*" }] } ]}
作用域也可以應用於特定的 fs
API,方法是使用 API 的識別符號而不是 fs:scope
{ "permissions": [ { "identifier": "fs:allow-exists", "allow": [{ "path": "$APPDATA/databases/*" }] } ]}
請注意 $APPDATA
變數的使用。該值在執行時注入,解析為應用程式資料目錄。
可用的變數包括:$APPCONFIG
、$APPDATA
、$APPLOCALDATA
、$APPCACHE
、$APPLOG
、$AUDIO
、$CACHE
、$CONFIG
、$DATA
、$LOCALDATA
、$DESKTOP
、$DOCUMENT
、$DOWNLOAD
、$EXE
、$FONT
、$HOME
、$PICTURE
、$PUBLIC
、$RUNTIME
、$TEMPLATE
、$VIDEO
、$RESOURCE
、$TEMP
。
嘗試使用未在作用域中配置的 URL 執行任何 API,將會導致 Promise 拒絕,因為存取被拒絕。
枚舉
BaseDirectory
自
2.0.0
枚舉成員
AppCache
AppCache: 16;
來源:未定義
AppConfig
AppConfig: 13;
來源:未定義
AppData
AppData: 14;
來源:未定義
AppLocalData
AppLocalData: 15;
來源:未定義
AppLog
AppLog: 17;
來源:未定義
Audio
Audio: 1;
來源:未定義
Cache
Cache: 2;
來源:未定義
Config
Config: 3;
來源:未定義
Data
Data: 4;
來源:未定義
Desktop
Desktop: 18;
來源:未定義
Document
Document: 6;
來源:未定義
Download
Download: 7;
來源:未定義
Executable
Executable: 19;
來源:未定義
Font
Font: 20;
來源:未定義
Home
Home: 21;
來源:未定義
LocalData
LocalData: 5;
來源:未定義
Picture
Picture: 8;
來源:未定義
Public
Public: 9;
來源:未定義
Resource
Resource: 11;
來源:未定義
Runtime
Runtime: 22;
來源:未定義
Temp
Temp: 12;
來源:未定義
Template
Template: 23;
來源:未定義
Video
Video: 10;
來源:未定義
SeekMode
枚舉成員
Current
Current: 1;
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L80
End
End: 2;
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L81
Start
Start: 0;
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L79
類別
FileHandle
用於讀取和寫入檔案的 Tauri 抽象層。
自
2.0.0
繼承自
Resource
建構函式
new FileHandle()
new FileHandle(rid): FileHandle
參數
參數 | 類型 |
---|---|
rid | 數字 |
返回
繼承自
Resource.constructor
來源:未定義
存取器
rid
取得簽名
get rid(): number
返回
數字
繼承自
Resource.rid
來源:未定義
方法
close()
close(): Promise<void>
從記憶體中銷毀並清理此資源。您不應再在此物件上呼叫任何方法,並且應丟棄對它的任何引用。
返回
Promise
<void
>
繼承自
Resource.close
來源:未定義
read()
read(buffer): Promise<null | number>
最多將 p.byteLength
個位元組讀取到 p
中。它會解析為讀取的位元組數(0
< n
<= p.byteLength
),如果遇到任何錯誤則會拒絕。即使 read()
解析為 n
< p.byteLength
,它也可能會在呼叫期間使用 p
的所有空間作為暫存空間。如果有些資料可用,但不足 p.byteLength
個位元組,read()
通常會解析為可用的資料,而不是等待更多資料。
當 read()
遇到檔案結尾條件時,它會解析為 EOF(null
)。
當 read()
遇到錯誤時,它會拒絕並返回錯誤。
呼叫者應始終處理在考慮 EOF(null
)之前傳回的 n
> 0
個位元組。這樣做可以正確處理在讀取某些位元組後發生的 I/O 錯誤,以及允許的 EOF 行為。
參數
參數 | 類型 |
---|---|
buffer | Uint8Array <ArrayBufferLike > |
返回
Promise
<null
| number
>
範例
import { open, BaseDirectory } from "@tauri-apps/plugin-fs"// if "$APPCONFIG/foo/bar.txt" contains the text "hello world":const file = await open("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });const buf = new Uint8Array(100);const numberOfBytesRead = await file.read(buf); // 11 bytesconst text = new TextDecoder().decode(buf); // "hello world"await file.close();
自
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L314
seek()
seek(offset, whence): Promise<number>
Seek 設定下一個 read()
或 write()
的偏移量為 offset,根據 whence
解釋:Start
表示相對於檔案開頭,Current
表示相對於目前偏移量,而 End
表示相對於結尾。Seek 解析為相對於檔案開頭的新偏移量。
搜尋到檔案開頭之前的偏移量是錯誤的。搜尋到任何正偏移量都是合法的,但後續在底層物件上進行 I/O 操作的行為取決於實作。它會傳回游標位置的數字。
參數
參數 | 類型 |
---|---|
offset | 數字 |
whence | SeekMode |
返回
Promise
<number
>
範例
import { open, SeekMode, BaseDirectory } from '@tauri-apps/plugin-fs';
// Given hello.txt pointing to file with "Hello world", which is 11 bytes long:const file = await open('hello.txt', { read: true, write: true, truncate: true, create: true, baseDir: BaseDirectory.AppLocalData });await file.write(new TextEncoder().encode("Hello world"));
// Seek 6 bytes from the start of the fileconsole.log(await file.seek(6, SeekMode.Start)); // "6"// Seek 2 more bytes from the current positionconsole.log(await file.seek(2, SeekMode.Current)); // "8"// Seek backwards 2 bytes from the end of the fileconsole.log(await file.seek(-2, SeekMode.End)); // "9" (e.g. 11-2)
await file.close();
自
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L369
stat()
stat(): Promise<FileInfo>
傳回此檔案的 FileInfo
。
返回
範例
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';const file = await open("file.txt", { read: true, baseDir: BaseDirectory.AppLocalData });const fileInfo = await file.stat();console.log(fileInfo.isFile); // trueawait file.close();
自
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L391
truncate()
truncate(len?): Promise<void>
截斷或擴展此檔案,以達到指定的 len
。如果未指定 len
,則會截斷整個檔案內容。
參數
參數 | 類型 |
---|---|
len ? | 數字 |
返回
Promise
<void
>
範例
import { open, BaseDirectory } from '@tauri-apps/plugin-fs';
// truncate the entire fileconst file = await open("my_file.txt", { read: true, write: true, create: true, baseDir: BaseDirectory.AppLocalData });await file.truncate();
// truncate part of the fileconst file = await open("my_file.txt", { read: true, write: true, create: true, baseDir: BaseDirectory.AppLocalData });await file.write(new TextEncoder().encode("Hello World"));await file.truncate(7);const data = new Uint8Array(32);await file.read(data);console.log(new TextDecoder().decode(data)); // Hello Wawait file.close();
自
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L423
write()
write(data): Promise<number>
從 data
將 data.byteLength
個位元組寫入到底層資料流。它會解析為從 data
寫入的位元組數(0
<= n
<= data.byteLength
),或拒絕並返回導致寫入提前停止的錯誤。write()
如果會解析為 n
< data.byteLength
,則必須拒絕並返回非 null 錯誤。write()
不得修改切片資料,即使是暫時性的。
參數
參數 | 類型 |
---|---|
data | Uint8Array <ArrayBufferLike > |
返回
Promise
<number
>
範例
import { open, write, BaseDirectory } from '@tauri-apps/plugin-fs';const encoder = new TextEncoder();const data = encoder.encode("Hello world");const file = await open("bar.txt", { write: true, baseDir: BaseDirectory.AppLocalData });const bytesWritten = await file.write(data); // 11await file.close();
自
2.0.0
來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L450
介面
CopyFileOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
fromPathBaseDir? | BaseDirectory | fromPath 的基本目錄。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L586 |
toPathBaseDir? | BaseDirectory | toPath 的基本目錄。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L588 |
CreateOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L463 |
DebouncedWatchOptions
自
2.0.0
繼承自
屬性
屬性 | 類型 | 描述 | 繼承自 | 定義於 |
---|---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄 | WatchOptions .baseDir | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1162 |
delayMs? | 數字 | 延遲防抖 | - | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1170 |
recursive? | boolean | 遞迴監看目錄 | WatchOptions .recursive | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160 |
DirEntry
磁碟項目,可以是檔案、目錄或符號連結。
這是 readDir
的結果。
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
isDirectory | boolean | 指定此項目是否為目錄。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L677 |
isFile | boolean | 指定此項目是否為檔案。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L679 |
isSymlink | boolean | 指定此項目是否為符號連結。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L681 |
name | string | 項目的名稱(帶有副檔名的檔案名稱或目錄名稱)。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L675 |
ExistsOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1127 |
FileInfo
FileInfo 描述檔案,由 stat
、lstat
或 fstat
傳回。
自
2.0.0
屬性
MkdirOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L631 |
mode? | 數字 | 建立目錄時要使用的權限(預設為 0o777 ,在進程的 umask 之前)。在 Windows 上忽略。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L625 |
recursive? | boolean | 預設為 false 。如果設定為 true ,表示也將建立任何中間目錄(如同 shell 命令 mkdir -p )。 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L629 |
OpenOptions
自
2.0.0
屬性
ReadDirOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L663 |
ReadFileOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L725 |
RemoveOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L862 |
recursive? | boolean | 預設值為 false 。若設為 true ,即使路徑是非空目錄也會被移除。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L860 |
RenameOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
newPathBaseDir? | BaseDirectory | 用於 newPath 的基礎目錄。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L898 |
oldPathBaseDir? | BaseDirectory | 用於 oldPath 的基礎目錄。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L896 |
StatOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L940 |
TruncateOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L999 |
WatchEvent
自
2.0.0
屬性
WatchOptions
自
2.0.0
擴展自
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目錄 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1162 |
recursive? | boolean | 遞迴監看目錄 | 來源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160 |
WriteFileOptions
自
2.0.0
屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
append? | boolean | 預設值為 false 。若設為 true ,將會附加到檔案末尾,而不是覆寫先前的內容。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1043 |
baseDir? | BaseDirectory | path 的基本目錄 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1051 |
create? | boolean | 設定選項以允許建立新檔案,如果指定路徑上尚不存在檔案(預設值為 true )。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1045 |
createNew? | boolean | 設定選項以建立新檔案,若檔案已存在則會失敗。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1047 |
mode? | 數字 | 檔案權限。在 Windows 上會被忽略。 | Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1049 |
類型別名
UnwatchFn()
type UnwatchFn: () => void;
返回
void
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1243
WatchEventKind
type WatchEventKind: | "any" | object | object | object | object | "other";
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1185
WatchEventKindAccess
type WatchEventKindAccess: object | object | object | object;
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1196
WatchEventKindCreate
type WatchEventKindCreate: object | object | object | object;
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1205
WatchEventKindModify
type WatchEventKindModify: | object | object | object | object | object;
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1214
WatchEventKindRemove
type WatchEventKindRemove: object | object | object | object;
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1234
函數
copyFile()
function copyFile( fromPath, toPath,options?): Promise<void>
將一個檔案的內容和權限複製到另一個指定的路徑,預設情況下,如果需要會建立新檔案,否則會覆寫。
參數
參數 | 類型 |
---|---|
fromPath | string | URL |
toPath | string | URL |
options ? | CopyFileOptions |
返回
Promise
<void
>
範例
import { copyFile, BaseDirectory } from '@tauri-apps/plugin-fs';await copyFile('app.conf', 'app.conf.bk', { fromPathBaseDir: BaseDirectory.AppConfig, toPathBaseDir: BaseDirectory.AppConfig });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L601
create()
function create(path, options?): Promise<FileHandle>
若檔案不存在則建立檔案,或截斷現有檔案,並解析為 FileHandle
的實例。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | CreateOptions |
返回
範例
import { create, BaseDirectory } from "@tauri-apps/plugin-fs"const file = await create("foo/bar.txt", { baseDir: BaseDirectory.AppConfig });await file.write(new TextEncoder().encode("Hello world"));await file.close();
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L480
exists()
function exists(path, options?): Promise<boolean>
檢查路徑是否存在。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | ExistsOptions |
返回
Promise
<boolean
>
範例
import { exists, BaseDirectory } from '@tauri-apps/plugin-fs';// Check if the `$APPDATA/avatar.png` file existsawait exists('avatar.png', { baseDir: BaseDirectory.AppData });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1141
lstat()
function lstat(path, options?): Promise<FileInfo>
解析為指定 path
的 FileInfo
。如果 path
是符號連結,則會傳回符號連結的資訊,而不是它指向的目標。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | StatOptions |
返回
範例
import { lstat, BaseDirectory } from '@tauri-apps/plugin-fs';const fileInfo = await lstat("hello.txt", { baseDir: BaseDirectory.AppLocalData });console.log(fileInfo.isFile); // true
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L982
mkdir()
function mkdir(path, options?): Promise<void>
使用指定的路徑建立新目錄。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | MkdirOptions |
返回
Promise
<void
>
範例
import { mkdir, BaseDirectory } from '@tauri-apps/plugin-fs';await mkdir('users', { baseDir: BaseDirectory.AppLocalData });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L644
open()
function open(path, options?): Promise<FileHandle>
開啟檔案並解析為 FileHandle
的實例。如果使用 create
或 createNew
開啟選項,檔案不需事先存在。呼叫者有責任在使用完畢後關閉檔案。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | OpenOptions |
返回
範例
import { open, BaseDirectory } from "@tauri-apps/plugin-fs"const file = await open("foo/bar.txt", { read: true, write: true, baseDir: BaseDirectory.AppLocalData });// Do work with fileawait file.close();
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L565
readDir()
function readDir(path, options?): Promise<DirEntry[]>
讀取給定路徑的目錄,並傳回 DirEntry
的陣列。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | ReadDirOptions |
返回
範例
import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';import { join } from '@tauri-apps/api/path';const dir = "users"const entries = await readDir('users', { baseDir: BaseDirectory.AppLocalData });processEntriesRecursively(dir, entries);async function processEntriesRecursively(parent, entries) { for (const entry of entries) { console.log(`Entry: ${entry.name}`); if (entry.isDirectory) { const dir = await join(parent, entry.name); processEntriesRecursively(dir, await readDir(dir, { baseDir: BaseDirectory.AppLocalData })) } }}
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L706
readFile()
function readFile(path, options?): Promise<Uint8Array>
讀取檔案的完整內容,並解析為位元組陣列。如果需要,可以使用 TextDecoder 將位元組轉換為字串。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | ReadFileOptions |
返回
範例
import { readFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = await readFile('avatar.png', { baseDir: BaseDirectory.Resource });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L739
readTextFile()
function readTextFile(path, options?): Promise<string>
讀取檔案的完整內容,並以 UTF-8 字串形式傳回。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | ReadFileOptions |
返回
Promise
<string
>
範例
import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';const contents = await readTextFile('app.conf', { baseDir: BaseDirectory.AppConfig });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L765
readTextFileLines()
function readTextFileLines(path, options?): Promise<AsyncIterableIterator<string>>
傳回一個異步 AsyncIterableIterator,用於逐行讀取檔案內容,以 UTF-8 字串形式呈現。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | ReadFileOptions |
返回
Promise
<AsyncIterableIterator
<string
>>
範例
import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';const lines = await readTextFileLines('app.conf', { baseDir: BaseDirectory.AppConfig });for await (const line of lines) { console.log(line);}
您也可以呼叫 AsyncIterableIterator.next 來推進迭代器,以便在您想要時延遲讀取下一行。
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L798
remove()
function remove(path, options?): Promise<void>
移除指定的檔案或目錄。如果目錄非空且 recursive
選項未設為 true,Promise 將會被拒絕。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | RemoveOptions |
返回
Promise
<void
>
範例
import { remove, BaseDirectory } from '@tauri-apps/plugin-fs';await remove('users/file.txt', { baseDir: BaseDirectory.AppLocalData });await remove('users', { baseDir: BaseDirectory.AppLocalData });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L877
rename()
function rename( oldPath, newPath,options?): Promise<void>
將 oldpath 重新命名(移動)到 newpath。路徑可以是檔案或目錄。如果 newpath 已存在且不是目錄,rename() 會取代它。當 oldpath 和 newpath 位於不同目錄時,可能會套用作業系統特定的限制。
在 Unix 系統上,此操作不會追蹤任一路徑的符號連結。
參數
參數 | 類型 |
---|---|
oldPath | string | URL |
newPath | string | URL |
options ? | RenameOptions |
返回
Promise
<void
>
範例
import { rename, BaseDirectory } from '@tauri-apps/plugin-fs';await rename('avatar.png', 'deleted.png', { oldPathBaseDir: BaseDirectory.App, newPathBaseDir: BaseDirectory.AppLocalData });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L916
size()
function size(path): Promise<number>
取得檔案或目錄的大小。對於檔案,也可以使用 stat
函數。
如果 path
是目錄,此函數將會遞迴地迭代 path
內部的每個檔案和每個目錄,因此如果用於較大的目錄,將會非常耗時。
參數
參數 | 類型 |
---|---|
path | string | URL |
返回
Promise
<number
>
範例
import { size, BaseDirectory } from '@tauri-apps/plugin-fs';// Get the size of the `$APPDATA/tauri` directory.const dirSize = await size('tauri', { baseDir: BaseDirectory.AppData });console.log(dirSize); // 1024
自
2.1.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1340
stat()
function stat(path, options?): Promise<FileInfo>
解析為指定 path
的 FileInfo
。將始終追蹤符號連結,但如果符號連結指向範圍外的路徑,則會拒絕。
參數
參數 | 類型 |
---|---|
path | string | URL |
options ? | StatOptions |
返回
範例
import { stat, BaseDirectory } from '@tauri-apps/plugin-fs';const fileInfo = await stat("hello.txt", { baseDir: BaseDirectory.AppLocalData });console.log(fileInfo.isFile); // true
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L956
truncate()
function truncate( path, len?,options?): Promise<void>
截斷或擴展指定的檔案,以達到指定的 len
長度。如果 len
為 0
或未指定,則會截斷整個檔案內容。
參數
參數 | 類型 |
---|---|
path | string | URL |
len ? | 數字 |
options ? | TruncateOptions |
返回
Promise
<void
>
範例
import { truncate, readTextFile, writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';// truncate the entire fileawait truncate("my_file.txt", 0, { baseDir: BaseDirectory.AppLocalData });
// truncate part of the fileconst filePath = "file.txt";await writeTextFile(filePath, "Hello World", { baseDir: BaseDirectory.AppLocalData });await truncate(filePath, 7, { baseDir: BaseDirectory.AppLocalData });const data = await readTextFile(filePath, { baseDir: BaseDirectory.AppLocalData });console.log(data); // "Hello W"
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1022
watch()
function watch( paths, cb,options?): Promise<UnwatchFn>
監看檔案或目錄的變更(延遲後)。
參數
參數 | 類型 |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
options ? | DebouncedWatchOptions |
返回
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1254
watchImmediate()
function watchImmediate( paths, cb,options?): Promise<UnwatchFn>
監看檔案或目錄的變更。
參數
參數 | 類型 |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
options ? | WatchOptions |
返回
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1292
writeFile()
function writeFile( path, data,options?): Promise<void>
將 data
寫入給定的 path
,預設情況下,如果需要會建立新檔案,否則會覆寫。
參數
參數 | 類型 |
---|---|
path | string | URL |
data | Uint8Array <ArrayBufferLike > | ReadableStream <Uint8Array <ArrayBufferLike >> |
options ? | WriteFileOptions |
返回
Promise
<void
>
範例
import { writeFile, BaseDirectory } from '@tauri-apps/plugin-fs';
let encoder = new TextEncoder();let data = encoder.encode("Hello World");await writeFile('file.txt', data, { baseDir: BaseDirectory.AppLocalData });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1067
writeTextFile()
function writeTextFile( path, data,options?): Promise<void>
將 UTF-8 字串 data
寫入給定的 path
,預設情況下,如果需要會建立新檔案,否則會覆寫。
參數
參數 | 類型 |
---|---|
path | string | URL |
data | string |
options ? | WriteFileOptions |
返回
Promise
<void
>
範例
import { writeTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
await writeTextFile('file.txt', "Hello world", { baseDir: BaseDirectory.AppLocalData });
自
2.0.0
Source: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1103
© 2025 Tauri Contributors. CC-BY / MIT