@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 会导致承诺被拒绝,因为访问被拒绝。
枚举
BaseDirectory
自
2.0.0
枚举成员
AppCache
AppCache: 16;
参见
appCacheDir 了解更多信息。
来源: 未定义
AppConfig
AppConfig: 13;
参见
appConfigDir 了解更多信息。
来源: 未定义
AppData
AppData: 14;
参见
appDataDir 了解更多信息。
来源: 未定义
AppLocalData
AppLocalData: 15;
参见
appLocalDataDir 了解更多信息。
来源: 未定义
AppLog
AppLog: 17;
参见
appLogDir 了解更多信息。
来源: 未定义
Audio
Audio: 1;
参见
audioDir 了解更多信息。
来源: 未定义
Cache
Cache: 2;
参见
cacheDir 了解更多信息。
来源: 未定义
Config
Config: 3;
参见
configDir 了解更多信息。
来源: 未定义
Data
Data: 4;
参见
dataDir 了解更多信息。
来源: 未定义
Desktop
Desktop: 18;
参见
desktopDir 了解更多信息。
来源: 未定义
Document
Document: 6;
参见
documentDir 了解更多信息。
来源: 未定义
Download
Download: 7;
参见
downloadDir 了解更多信息。
来源: 未定义
Executable
Executable: 19;
参见
executableDir 了解更多信息。
来源: 未定义
Font
Font: 20;
参见
fontDir 了解更多信息。
来源: 未定义
Home
Home: 21;
参见
homeDir 了解更多信息。
来源: 未定义
LocalData
LocalData: 5;
参见
localDataDir 了解更多信息。
来源: 未定义
Picture
Picture: 8;
参见
pictureDir 了解更多信息。
来源: 未定义
Public
Public: 9;
参见
publicDir 了解更多信息。
来源: 未定义
Resource
Resource: 11;
参见
resourceDir 了解更多信息。
来源: 未定义
Runtime
Runtime: 22;
参见
runtimeDir 了解更多信息。
来源: 未定义
Temp
Temp: 12;
参见
tempDir 了解更多信息。
来源: 未定义
Template
Template: 23;
参见
templateDir 了解更多信息。
来源: 未定义
Video
Video: 10;
参见
videoDir 了解更多信息。
来源: 未定义
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 | number |
返回值
继承自
Resource.constructor
来源: 未定义
访问器
rid
获取签名
get rid(): number
返回值
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 | number |
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 ? | number |
返回值
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
,则必须拒绝并返回非空错误。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#L1170 |
delayMs? | number | 防抖延迟 | - | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1178 |
recursive? | boolean | 递归地监视目录 | WatchOptions .recursive | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1168 |
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#L1135 |
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? | number | 创建目录时要使用的权限(默认为 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 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L663 |
ReadFileOptions
自
2.0.0
属性
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L725 |
RemoveOptions
自
2.0.0
属性
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L862 |
recursive? | boolean | 默认为 false 。如果设置为 true ,即使路径是非空目录,也会将其删除。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L860 |
RenameOptions
自
2.0.0
属性
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
newPathBaseDir? | BaseDirectory | newPath 的基本目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L898 |
oldPathBaseDir? | BaseDirectory | oldPath 的基本目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L896 |
StatOptions
自
2.0.0
属性
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L940 |
TruncateOptions
自
2.0.0
属性
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录。 | 来源: 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#L1170 |
recursive? | boolean | 递归地监视目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1168 |
WriteFileOptions
自
2.0.0
属性
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
append? | boolean | 默认为 false 。如果设置为 true ,将追加到文件末尾,而不是覆盖先前的内容。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1043 |
baseDir? | BaseDirectory | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1051 |
create? | boolean | 设置选项以允许创建新文件,如果指定路径上尚不存在文件(默认为 true )。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1045 |
createNew? | boolean | 设置选项以创建新文件,如果文件已存在则操作失败。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1047 |
mode? | number | 文件权限。在 Windows 上忽略。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1049 |
类型别名
UnwatchFn()
type UnwatchFn: () => void;
返回值
void
自
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1251
WatchEventKind
type WatchEventKind: | "any" | object | object | object | object | "other";
自
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1193
WatchEventKindAccess
type WatchEventKindAccess: object | object | object | object;
自
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1204
WatchEventKindCreate
type WatchEventKindCreate: object | object | object | object;
自
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1213
WatchEventKindModify
type WatchEventKindModify: | object | object | object | object | object;
自
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1222
WatchEventKindRemove
type WatchEventKindRemove: object | object | object | object;
自
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1242
函数
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
来源: 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
来源: 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
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1149
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
来源: 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
来源: 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
来源: 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
来源: 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
来源: 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
来源: 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
来源: 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
来源: 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
来源: 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
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1348
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
来源: 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 ? | number |
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
来源: 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
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1262
watchImmediate()
function watchImmediate( paths, cb,options?): Promise<UnwatchFn>
监视文件或目录上的更改。
参数
参数 | 类型 |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
options ? | WatchOptions |
返回值
自
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1300
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
来源: 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
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1111
© 2025 Tauri Contributors. CC-BY / MIT