跳转至

File

继承

Reference

简要描述

用来处理文件读写的类。

描述

文件类。它用于将数据永久存储到用户设备的文件系统中,并从中读取数据。这可以用来存储游戏保存数据或播放器配置文件,下面是一个如何读写文件的示例:

func save(content):
    var file = File.new()
    file.open("user://save_game.dat", File.WRITE)
    file.store_string(content)
    file.close()

func load():
    var file = File.new()
    file.open("user://save_game.dat", File.READ)
    var content = file.get_as_text()
    file.close()
    return content

成员

类型 属性名 默认值
bool endian_swap false

方法

返回值类型 方法名称
void close()
bool eof_reached() const
bool file_exists(path: String) const
int get_16() const
int get_32() const
int get_64() const
int get_8() const
String get_as_text() const
PoolByteArray get_buffer(len: int) const
PoolStringArray get_csv_line(delim: String = ",") const
float get_double() const
int get_error() const
float get_float() const
int get_len() const
String get_line() const
String get_md5(path: String) const
int get_modified_time(file: String) const
String get_pascal_string()
String get_path() const
String get_path_absolute() const
int get_position() const
float get_real() const
String get_sha256(path: String) const
Variant get_var(allow_objects: bool = false) const
bool is_open() const
int open(path: String, flags: int)
int open_compressed(path: String, mode_flags: int, compression_mode: int = 0)
int open_encrypted(path: String, mode_flags: int, key: PoolByteArray)
int open_encrypted_with_pass(path: String, mode_flags: int, pass: String)
void seek(position: int)
void seek_end(position: int = 0)
void store_16(value: int)
void store_32(value: int)
void store_64(value: int)
void store_8(value: int)
void store_buffer(buffer: PoolByteArray)
void store_csv_line(values: PoolStringArray, delim: String = ",")
void store_double(value: float)
void store_float(value: float)
void store_line(line: String)
void store_pascal_string(string: String)
void store_real(value: float)
void store_string(string: String)
void store_var(value: Variant, full_objects: bool = false)
##枚举
enum ModeFlags:
- READ = 1

打开文件进行读取操作。

  • WRITE = 2

打开文件进行写操作。 如果文件不存在,则创建它,如果存在,则截断它。

  • READ_WRITE = 3

打开文件进行读写操作。 不截断文件。

  • WRITE_READ = 7

打开文件进行读写操作。 如果文件不存在,则创建它,如果存在,则截断它。


enum CompressionMode: - COMPRESSION_FASTLZ = 0

使用[=] FastLZ [/url]压缩方法。

  • COMPRESSION_DEFLATE = 1

使用[=] DEFLATE [/url]压缩方法。

  • COMPRESSION_ZSTD = 2

使用[=] Zstandard [/url]压缩方法。

  • COMPRESSION_GZIP = 3

使用[=] gzip [/url]压缩方法。


常量

成员说明

  • bool endian_swap
Default false
setter set_endian_swap(value)
getter get_endian_swap

方法说明

  • close close()

关闭当前打开的文件。


  • eof_reached eof_reached() const

如果文件游标已读取超过文件末尾,则返回true

**注意:**这个函数在文件结束时仍然返回false,并且只有当读取超过它时才会激活。这可能令人困惑,但它符合所有操作系统中底层文件访问的工作方式。总是有 get_lenget_position来实现自定义逻辑。


  • file_exists file_exists(path: String) const

如果文件存在于给定的路径中,则返回true

**注意:**许多资源类型被导入(例如,纹理或声音文件),并且它们的源资产将不包含在导出的游戏中,因为仅使用了导入的版本(在文件夹 res://.import 中)。要检查这些资源是否存在,同时考虑到它们导入位置的重新映射,请使用 ResourceLoader.exists。通常,当您在编辑器中进行开发时,使用File.file_exists导入资源上的将成功(源资产存在于res://中,但在导出时失败)。


  • get_16 get_16() const

以整数形式返回文件中的后16位数据。


  • get_32 get_32() const

以整数形式返回文件中的后32位数据。


  • get_64 get_64() const

以整数形式返回文件中的后64位数据。


  • get_8 get_8() const

以整数形式返回文件中的后8位数据。


  • get_as_text get_as_text() const

String 形式返回整个文件。

文本被解释为UTF-8编码。


  • get_buffer get_buffer(len: int) const

返回文件的下一个len个字节,作为 PoolByteArray


  • get_csv_line get_csv_line(delim: String = ",") const

以CSV(逗号分隔值)格式返回文件的下一个值。 您可以传递一个不同的定界符delim以使用默认的","(逗号)以外的字符。 该定界符必须为一个字符长。

文本被解释为UTF-8编码。


  • get_double get_double() const

以浮点数的形式返回文件的下一个64位。


  • get_error get_error() const

返回尝试执行操作时发生的最后一个错误。 与 Error 中的ERR_FILE _ *常量进行比较。


  • get_float get_float() const

返回文件中的后32位作为浮点数。


  • get_len get_len() const

返回文件的大小(以字节为单位)。


  • get_line get_line() const

String 返回文件的下一行。

文本被解释为UTF-8编码。


  • get_md5 get_md5(path: String) const

返回表示给定路径中文件的MD5字符串,失败时返回空 String


  • get_modified_time get_modified_time(file: String) const

返回上次以unix时间戳格式修改file的时间,或者返回String" ERROR IN file"。 可以使用OS.get_datetime_from_unix_time将这个unix时间戳转换为日期时间。


  • get_pascal_string get_pascal_string()

从文件返回以Pascal格式保存的String

文本被解释为UTF-8编码。


  • get_path get_path() const

返回当前打开文件的路径作为String


  • get_path_absolute get_path_absolute() const

String返回当前打开文件的绝对路径。


  • get_position get_position() const

返回文件光标的位置。


  • get_real get_real() const

返回文件中的下一位作为浮点数。


  • get_sha256 get_sha256(path: String) const

返回表示给定路径中文件的SHA-256 String,失败时返回空的String


  • get_var get_var(allow_objects: bool = false) const

返回文件中的下一个Variant值。 如果allow_objectstrue,则允许解码对象。

**警告:**反序列化的对象可能包含要执行的代码。 如果序列化的对象来自不受信任的来源,请不要使用此选项,以避免潜在的安全威胁,例如远程执行代码。


  • is_open is_open() const

如果当前正在打开文件,则返回true


  • open open(path: String, flags: int)

打开文件以进行写入或读取,具体取决于标志。


  • open_compressed open_compressed(path: String, mode_flags: int, compression_mode: int = 0)

打开一个压缩文件以进行读取或写入。


  • open_encrypted open_encrypted(path: String, mode_flags: int, key: PoolByteArray)

以写或读模式打开加密的文件。 您需要传递一个二进制密钥来对其进行加密/解密。


  • open_encrypted_with_pass open_encrypted_with_pass(path: String, mode_flags: int, pass: String)

以写或读模式打开加密的文件。 您需要输入密码来加密/解密。


  • seek seek(position: int)

将文件读/写光标更改到指定位置(从文件开头开始的字节数)。


  • seek_end seek_end(position: int = 0)

将文件读/写光标更改到指定位置(从文件末尾开始的字节数)。

**注意:**这是一个偏移量,因此您应该使用负数,否则光标将位于文件的末尾。


  • store_16 store_16(value: int)

将整数作为16位存储在文件中。


  • store_32 store_32(value: int)

将整数作为32位存储在文件中。


  • store_64 store_64(value: int)

将整数作为64位存储在文件中。


  • store_8 store_8(value: int)

将整数作为8位存储在文件中。


  • store_buffer store_buffer(buffer: PoolByteArray)

将给定的字节数组存储在文件中。


  • store_csv_line store_csv_line(values: PoolStringArray, delim: String = ",")

将给定的PoolStringArray存储为以CSV(逗号分隔值)格式设置的行。 您可以传递一个不同的定界符delim以使用默认的","(逗号)以外的字符。 该定界符必须为一个字符长。

文本将被编码为UTF-8。


  • store_double store_double(value: float)

在文件中将浮点数存储为64位。


  • store_float store_float(value: float)

在文件中将浮点数存储为32位。


  • store_line store_line(line: String)

将给定的String作为行存储在文件中。

文本将被编码为UTF-8。


  • store_pascal_string store_pascal_string(string: String)

以Pascal格式将给定的String作为行存储在文件中(即,还存储字符串的长度)。

文本将被编码为UTF-8。


  • store_real store_real(value: float)

将浮点数存储在文件中。


  • store_string store_string(string: String)

将给定的String存储在文件中。

文本将被编码为UTF-8。


  • store_var store_var(value: Variant, full_objects: bool = false)

将所有Variant值存储在文件中。 如果full_objectstrue,则允许编码对象(并可能包含代码)。