@tauri-apps/plugin-fs
访问文件系统。
此模块可防止路径遍历,不允许使用父目录访问器(即不允许使用“/usr/path/to/../file”或“../path/to/file”路径)。使用此 API 访问的路径必须是相对于其中一个基本目录的路径,或者使用路径 API 创建的路径。
该 API 具有作用域配置,强制您使用 glob 模式限制可以访问的路径。
作用域配置是一个 glob 模式数组,描述允许的文件/目录路径。例如,此作用域配置允许所有启用的 fs
API(仅)访问$APPDATA
目录的数据库目录中的文件。
{ "permissions": [ { "identifier": "fs:scope", "allow": [{ "path": "$APPDATA/databases/*" }] } ]}
作用域也可以通过使用 API 的标识符而不是 fs:scope
应用于特定的 fs
API。
{ "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 拒绝。
2.0.0
AppCache: 16;
有关更多信息,请参阅 appCacheDir。
来源: 未定义
AppConfig: 13;
有关更多信息,请参阅 appConfigDir。
来源: 未定义
AppData: 14;
有关更多信息,请参阅 appDataDir。
来源: 未定义
AppLocalData: 15;
有关更多信息,请参阅 appLocalDataDir。
来源: 未定义
AppLog: 17;
有关更多信息,请参阅 appLogDir。
来源: 未定义
Audio: 1;
有关更多信息,请参阅 audioDir。
来源: 未定义
Cache: 2;
有关更多信息,请参阅 cacheDir。
来源: 未定义
Config: 3;
有关更多信息,请参阅 configDir。
来源: 未定义
Data: 4;
有关更多信息,请参阅 dataDir。
来源: 未定义
Desktop: 18;
有关更多信息,请参阅 desktopDir。
来源: 未定义
Document: 6;
有关更多信息,请参阅 documentDir。
来源: 未定义
Download: 7;
有关更多信息,请参阅 downloadDir。
来源: 未定义
Executable: 19;
有关更多信息,请参阅 executableDir。
来源: 未定义
Font: 20;
有关更多信息,请参阅 fontDir。
来源: 未定义
Home: 21;
有关更多信息,请参阅 homeDir。
来源: 未定义
LocalData: 5;
有关更多信息,请参阅 localDataDir。
来源: 未定义
Picture: 8;
有关更多信息,请参阅 pictureDir。
来源: 未定义
Public: 9;
有关更多信息,请参阅 publicDir。
来源: 未定义
Resource: 11;
有关更多信息,请参阅 resourceDir。
来源: 未定义
Runtime: 22;
有关更多信息,请参阅 runtimeDir。
来源: 未定义
Temp: 12;
有关更多信息,请参阅 tempDir。
来源: 未定义
Template: 23;
有关更多信息,请参阅 templateDir。
来源: 未定义
Video: 10;
有关更多信息,请参阅 videoDir。
来源: 未定义
Current: 1;
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L80
End: 2;
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L81
Start: 0;
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L79
Tauri 用于读写文件的抽象。
2.0.0
Resource
new FileHandle(rid): FileHandle
参数 | 类型 |
---|---|
rid | 数字 |
Resource.constructor
来源: 未定义
get rid(): number
数字
Resource.rid
来源: 未定义
close(): Promise<void>
销毁并从内存中清理此资源。您不应再对此对象调用任何方法,并应放弃对它的任何引用。
Promise
<void
>
Resource.close
来源: 未定义
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 行为。
参数 | 类型 |
---|---|
缓冲区 | Uint8Array |
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(offset, whence): Promise<number>
Seek 根据 whence
设置下一个 read()
或 write()
的偏移量:Start
表示相对于文件开头,Current
表示相对于当前偏移量,End
表示相对于文件末尾。Seek 解析为相对于文件开头的新偏移量。
定位到文件开头之前的偏移量是错误的。定位到任何正偏移量都是合法的,但后续 I/O 操作对底层对象的行为取决于实现。它返回光标位置的数量。
参数 | 类型 |
---|---|
偏移量 | 数字 |
原因 | 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(): 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(len?): Promise<void>
截断或扩展此文件,以达到指定的 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(data): Promise<number>
将 data.byteLength
字节从 data
写入底层数据流。它解析为从 data
写入的字节数(0
<= n
<= data.byteLength
),或者在写入过早停止时以遇到的错误拒绝。write()
如果解析为 n
< data.byteLength
,则必须以非 null 错误拒绝。write()
决不能修改切片数据,即使是临时修改也不可以。
参数 | 类型 |
---|---|
数据 | Uint8Array |
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
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 |
2.0.0
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L463 |
2.0.0
属性 | 类型 | 描述 | 继承自 (Inherited from) | 定义于 |
---|---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录 | WatchOptions .baseDir | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1170 |
delayMs? | 数字 | 去抖延迟 | - | 来源: 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 |
一个磁盘条目,可以是文件、目录或符号链接。
这是 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 | 字符串 | 条目的名称(带扩展名的文件名或目录名)。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L675 |
2.0.0
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1135 |
FileInfo 描述一个文件,由 stat
、lstat
或 fstat
返回。
2.0.0
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 |
2.0.0
2.0.0
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L663 |
2.0.0
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L725 |
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 |
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 |
2.0.0
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录。 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L940 |
2.0.0
属性 | 类型 | 描述 | 定义于 |
---|---|---|---|
baseDir? | BaseDirectory | path 的基本目录。 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L999 |
2.0.0
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 |
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? | 数字 | 文件权限。在 Windows 上被忽略。 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1049 |
type UnwatchFn: () => void;
空
2.0.0
来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1252
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
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
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
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
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
function copyFile( fromPath, toPath,options?): Promise<void>
将一个文件的内容和权限复制到另一个指定路径,默认情况下如果需要则创建新文件,否则覆盖。
参数 | 类型 |
---|---|
fromPath | string | URL |
toPath | string | URL |
选项 ? | 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
function create(path, options?): Promise<FileHandle>
如果文件不存在,则创建文件;如果文件已存在,则截断文件,并解析为 FileHandle
的实例。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function exists(path, options?): Promise<boolean>
检查路径是否存在。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function lstat(path, options?): Promise<FileInfo>
解析为指定 path
的 FileInfo
。如果 path
是符号链接,则返回符号链接的信息而不是其指向的内容。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function mkdir(path, options?): Promise<void>
创建具有指定路径的新目录。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function open(path, options?): Promise<FileHandle>
打开文件并解析为 FileHandle
的实例。如果使用 create
或 createNew
打开选项,则文件不需要事先存在。调用者有责任在文件使用完毕后关闭它。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function readDir(path, options?): Promise<DirEntry[]>
读取给定路径的目录并返回一个 DirEntry
数组。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function readFile(path, options?): Promise<Uint8Array>
读取文件所有内容并解析为字节数组。如果需要,可以使用 TextDecoder 将字节转换为字符串。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function readTextFile(path, options?): Promise<string>
读取文件所有内容并返回 UTF-8 字符串。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function readTextFileLines(path, options?): Promise<AsyncIterableIterator<string>>
返回一个异步 AsyncIterableIterator,以 UTF-8 字符串形式迭代文件的行。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function remove(path, options?): Promise<void>
删除指定的文件或目录。如果目录不为空且未将 recursive
选项设置为 true,则 promise 将被拒绝。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function rename( oldPath, newPath,options?): Promise<void>
将 oldpath 重命名(移动)到 newpath。路径可以是文件或目录。如果 newpath 已存在且不是目录,则 rename() 将替换它。当 oldpath 和 newpath 位于不同的目录中时,可能会应用操作系统特定的限制。
在 Unix 上,此操作不遵循任何路径上的符号链接。
参数 | 类型 |
---|---|
oldPath | string | URL |
newPath | string | URL |
选项 ? | 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
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#L1334
function stat(path, options?): Promise<FileInfo>
解析为指定 path
的 FileInfo
。将始终遵循符号链接,但如果符号链接指向范围之外的路径,则会拒绝。
参数 | 类型 |
---|---|
path | string | URL |
选项 ? | 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
function truncate( path, len?,options?): Promise<void>
截断或扩展指定文件,以达到指定的 len
。如果 len
为 0
或未指定,则整个文件内容将被截断。
参数 | 类型 |
---|---|
path | string | URL |
长度 ? | 数字 |
选项 ? | 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
function watch( paths, cb,options?): Promise<UnwatchFn>
观察文件或目录的变化(延迟后)。
参数 | 类型 |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
选项 ? | DebouncedWatchOptions |
2.0.0
来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1291
function watchImmediate( paths, cb,options?): Promise<UnwatchFn>
观察文件或目录的变化。
参数 | 类型 |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
选项 ? | WatchOptions |
2.0.0
来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1308
function writeFile( path, data,options?): Promise<void>
将 data
写入给定 path
,默认情况下如果需要则创建新文件,否则覆盖。
参数 | 类型 |
---|---|
path | string | URL |
数据 | Uint8Array | ReadableStream <Uint8Array > |
选项 ? | 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
function writeTextFile( path, data,options?): Promise<void>
将 UTF-8 字符串 data
写入给定 path
,默认情况下如果需要则创建新文件,否则覆盖。
参数 | 类型 |
---|---|
path | string | URL |
数据 | 字符串 |
选项 ? | 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 贡献者。CC-BY / MIT