@tauri-apps/plugin-fs
访问文件系统。
安全
此模块防止路径遍历,不允许使用父目录访问器(例如,“/usr/path/to/.. /file”或“../path/to/file”路径不被允许)。通过此API访问的路径必须相对位于某个基本目录之一,或者使用路径API创建。
API有一个作用域配置,该配置强制您通过glob模式限制可以访问的路径。
作用域配置是描述允许的文件/目录路径的glob模式的数组。例如,此作用域配置允许所有启用的fs
API(仅)访问$APPDATA
目录中的databases目录中的文件
{ "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;
出处: 未定义
应用程序配置
AppConfig: 13;
出处: 未定义
应用程序数据
AppData: 14;
出处: 未定义
应用程序本地数据
AppLocalData: 15;
出处: 未定义
应用程序日志
AppLog: 17;
出处: 未定义
音频
Audio: 1;
出处: 未定义
缓存
Cache: 2;
出处: 未定义
配置
Config: 3;
出处: 未定义
数据
Data: 4;
出处: 未定义
桌面
Desktop: 18;
出处: 未定义
文档
Document: 6;
出处: 未定义
下载
Download: 7;
出处: 未定义
可执行文件
Executable: 19;
出处: 未定义
字体
Font: 20;
出处: 未定义
主页
Home: 21;
出处: 未定义
本地数据
LocalData: 5;
出处: 未定义
图片
Picture: 8;
出处: 未定义
公共
Public: 9;
出处: 未定义
资源
Resource: 11;
出处: 未定义
运行时
Runtime: 22;
出处: 未定义
临时
Temp: 12;
出处: 未定义
模板
Template: 23;
出处: 未定义
视频
Video: 10;
出处: 未定义
搜索模式
枚举成员
当前
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
扩展
资源
构造函数
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行为。
参数
参数 | 类型 |
---|---|
缓冲区 | 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 函数设置 next read() 或 write() 操作的偏移量到 offset,根据 whence 的解释:Start
表示相对于文件开头,Current
表示相对于当前位置,End
表示相对于文件末尾。Seek 函数将返回相对于文件开头的新的偏移量。
尝试将偏移量设置为文件开始之前是一个错误。将偏移量设置到任何正值是合法的,但底层对象后续 I/O 操作的行为依赖于具体实现。它返回光标的当前位置。
参数
参数 | 类型 |
---|---|
offset | 数字 |
whence | 搜索模式 |
返回
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 时解析,则必须拒绝,并返回非空错误。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
接口
复制文件选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
fromPathBaseDir? | 基本目录 | fromPath 的基本目录。 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L586 |
toPathBaseDir? | 基本目录 | toPath 的基本目录。 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L588 |
创建选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
baseDir? | 基本目录 | path 的基本目录 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L463 |
去抖动监视选项
自从
2.0.0
扩展
属性
属性 | 类型 | 描述 | 继承自 | 在 |
---|---|---|---|---|
baseDir? | 基本目录 | 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? | 布尔值 | 递归监视目录 | 观察选项 .recursive | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160 |
目录条目
一个磁盘条目,可以是文件、目录或符号链接。
这是由readDir
的结果。
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
isDirectory | 布尔值 | 指定此条目是否为目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L677 |
isFile | 布尔值 | 指定此条目是否为文件。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L679 |
isSymlink | 布尔值 | 指定此条目是否为符号链接。 | 来源: 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? | 基本目录 | 基础目录用于path 。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1127 |
文件信息
FileInfo表示一个文件,并由stat
、lstat
或fstat
返回。
自从
2.0.0
属性
创建目录选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
baseDir? | 基本目录 | 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? | 布尔值 | 默认为 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? | 基本目录 | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L663 |
读取文件选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
baseDir? | 基本目录 | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L725 |
删除选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
baseDir? | 基本目录 | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L862 |
recursive? | 布尔值 | 默认值 false 。如果设置为 true ,路径将被删除,即使它是一个非空目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L860 |
重命名选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
newPathBaseDir? | 基本目录 | newPath 的基本目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L898 |
oldPathBaseDir? | 基本目录 | oldPath 的基本目录。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L896 |
状态选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
baseDir? | 基本目录 | 基础目录用于path 。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L940 |
截断选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
baseDir? | 基本目录 | 基础目录用于path 。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L999 |
监视事件
自从
2.0.0
属性
监视选项
自从
2.0.0
扩展由
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
baseDir? | 基本目录 | path 的基本目录 | 来源:https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1162 |
recursive? | 布尔值 | 递归监视目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1160 |
写文件选项
自从
2.0.0
属性
属性 | 类型 | 描述 | 在 |
---|---|---|---|
append? | 布尔值 | 默认值 false 。如果设置为 true ,将追加到文件中而不是覆盖以前的文件内容。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1043 |
baseDir? | 基本目录 | path 的基本目录 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1051 |
create? | 布尔值 | 设置选项以允许在指定的路径上创建新文件(默认值为 true )。 | 来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1045 |
createNew? | 布尔值 | 设置选项以创建新文件,如果它已存在则失败。 | 来源: 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#L1243
监视事件类型
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#L1185
监视事件类型访问
type WatchEventKindAccess: object | object | object | object;
自从
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1196
监视事件类型创建
type WatchEventKindCreate: object | object | object | object;
自从
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1205
监视事件类型修改
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#L1214
监视事件类型删除
type WatchEventKindRemove: object | object | object | object;
自从
2.0.0
来源: 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 | 字符串 | URL |
toPath | 字符串 | URL |
options ? | 复制文件选项 |
返回
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
实例。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 创建选项 |
返回
示例
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>
检查路径是否存在。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 存在选项 |
返回
Promise
<布尔值
>
示例
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#L1141
lstat()
function lstat(path, options?): Promise<FileInfo>
解析为指定 path
的 FileInfo
。如果 path
是符号链接,则返回符号链接的信息,而不是它指向的信息。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 状态选项 |
返回
示例
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>
使用指定的路径创建新的目录。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 创建目录选项 |
返回
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
打开选项,文件不需要先前存在。文件打开后完成时,关闭文件的调用方负责。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 打开选项 |
返回
示例
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
数组。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 读取目录选项 |
返回
示例
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 将字节转换为字符串。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 读取文件选项 |
返回
示例
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 字符串。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 读取文件选项 |
返回
Promise
<字符串
>
示例
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 字符串行。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 读取文件选项 |
返回
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 来 advancing the iterator 以便您可以随时延迟地读取下一行。
自从
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 将被拒绝。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 删除选项 |
返回
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>
重新命名(移动)旧路径到新路径。路径可以是文件或目录。如果新路径已存在且不是目录,rename() 会替换它。当旧路径和新路径位于不同的目录时,可能会应用操作系统特定的限制。
在 Unix 上,此操作在任一路径上都不会跟随符号链接。
参数
参数 | 类型 |
---|---|
oldPath | 字符串 | URL |
newPath | 字符串 | URL |
options ? | 重命名选项 |
返回
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 内的每个文件和每个目录中进行迭代,因此在较大目录上使用时可能会导致时间非常耗费。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | 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#L1340
stat()
function stat(path, options?): Promise<FileInfo>
解析特定 path 的 FileInfo
。始终跟随符号链接,但如果符号链接指向作用域之外的路由,则会拒绝。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
options ? | 状态选项 |
返回
示例
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 或未指定,则截断整个文件内容。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
len ? | 数字 |
options ? | 截断选项 |
返回
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 |
options ? | 去抖动监视选项 |
返回
自从
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1254
即时监视()
function watchImmediate( paths, cb,options?): Promise<UnwatchFn>
监视文件或目录上的更改。
参数
参数 | 类型 |
---|---|
paths | string | URL | string [] | URL [] |
cb | (event ) => void |
options ? | 监视选项 |
返回
自从
2.0.0
来源: https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/fs/guest-js/index.ts#L1292
writeFile()
function writeFile( path, data,options?): Promise<void>
将数据写入给定的 path,默认情况下,如果需要则创建新文件,否则覆盖。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
data | Uint8Array <ArrayBufferLike > | ReadableStream <Uint8Array <ArrayBufferLike >> |
options ? | 写文件选项 |
返回
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
,默认情况下如果需要,则创建新文件,否则覆盖。
参数
参数 | 类型 |
---|---|
路径 | 字符串 | URL |
data | 字符串 |
options ? | 写文件选项 |
返回
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#L1103
© 2025 Tauri 贡献者。CC-BY / MIT