文件

Inherits: Reference < Object

类别: 核心

简要说明

键入以处理文件读写操作。

属性

bool

endian_swap

方法

无效

close ( )

bool

eof_reached ( ) const

bool

file_exists ( String path ) 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 ( int len ) const

PoolStringArray

get_csv_line ( String delim="," ) const

float

get_double ( ) const

Error

get_error ( ) const

float

get_float ( ) const

int

get_len ( ) const

String

get_line ( ) const

String

get_md5 ( String path ) const

int

get_modified_time ( String file ) 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 ( String path ) const

Variant

get_var ( bool allow_objects=false ) const

bool

is_open ( ) const

Error

open ( String path, ModeFlags flags )

Error

open_compressed ( String path, ModeFlags mode_flags, CompressionMode compression_mode=0 )

Error

open_encrypted ( String path, ModeFlags mode_flags, PoolByteArray key )

Error

open_encrypted_with_pass ( String path, ModeFlags mode_flags, String pass )

无效

seek ( int position )

无效

seek_end ( int position=0 )

无效

store_16 ( int value )

无效

store_32 ( int value )

无效

store_64 ( int value )

无效

store_8 ( int value )

无效

store_buffer ( PoolByteArray buffer )

无效

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

无效

store_double ( float value )

无效

store_float ( float value )

无效

store_line ( String line )

无效

store_pascal_string ( String string )

无效

store_real ( float value )

无效

store_string ( String string )

无效

store_var ( Variant value, bool full_objects=false )

枚举

枚举 ModeFlags

  • READ = 1 ---打开文件进行读取操作。

  • WRITE = 2 ---打开文件进行写入操作。如果文件不存在,则创建它;如果文件存在,则截断它。

  • READ_WRITE = 3 ---打开文件进行读写操作。不截断文件。

  • WRITE_READ = 7 ---打开文件进行读写操作。如果文件不存在,则创建它;如果文件存在,则截断它。

枚举 CompressionMode

  • COMPRESSION_FASTLZ = 0 ---使用 FastLZ 压缩方法。

  • COMPRESSION_DEFLATE = 1 ---使用 DEFLATE 压缩方法。

  • COMPRESSION_ZSTD = 2 ---使用 Zstandard 压缩方法。

  • COMPRESSION_GZIP = 3 ---使用 gzip 压缩方法。

描述

文件类型。它用于将数据永久存储到用户设备的文件系统中并从中读取数据。例如,它可以用于存储游戏保存数据或玩家配置文件。

以下是有关如何从文件中写入和读取的示例:

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

属性描述

违约

设定器

设置“结束”交换(值)

吸气剂

获取u endian_swap()。

如果 true ,将交换文件的endianness。如果要处理在big endian机器上写入的文件,请使用此选项。

注: 这是关于文件格式,而不是CPU类型。这总是重置为 false 无论何时打开文件。

方法说明

  • void close ( )

关闭当前打开的文件。

  • bool eof_reached ( ) const

返回 true 如果文件光标的读数超过了文件末尾。

注: 此函数仍将返回 false 而在文件末尾,只有在读取文件时才会激活。这可能令人困惑,但它符合所有操作系统中低级文件访问的工作方式。总是有 get_lenget_position 实现自定义逻辑。

返回 true 如果文件存在于给定路径中。

注: 许多资源类型被导入(例如纹理或声音文件),并且它们的源资源不会包含在导出的游戏中,因为只使用导入的版本(在 res://.import 文件夹)。要在考虑重新映射到导入位置的同时检查是否存在此类资源,请使用 ResourceLoader.exists . 通常,使用 File.file_exists 在编辑器中开发时,导入的资源可以工作(源资源位于 res:// ,但导出时失败)。

  • int get_16 ( ) const

以整数形式返回文件中接下来的16位。

  • int get_32 ( ) const

以整数形式返回文件中接下来的32位。

  • int get_64 ( ) const

以整数形式返回文件中接下来的64位。

  • int get_8 ( ) const

以整数形式返回文件中接下来的8位。

  • String get_as_text ( ) const

将整个文件作为 String .

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

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

以csv(逗号分隔值)格式返回文件的下一个值。可以传递其他分隔符 delim 使用非默认值 "," (逗号)。此分隔符的长度必须为一个字符。

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

  • float get_double ( ) const

以浮点数形式返回文件中接下来的64位。

  • Error get_error ( ) const

返回尝试执行操作时发生的上一个错误。与 ERR_FILE_* 常量来自 Error .

  • float get_float ( ) const

以浮点数形式返回文件中接下来的32位。

  • int get_len ( ) const

返回文件的大小(字节)。

将文件的下一行作为 String .

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

返回一个MD5字符串,该字符串表示给定路径上的文件或空文件 String 失败论。

  • int get_modified_time ( String file ) const

返回上一次 file 以unix时间戳格式修改或返回 String “错误 file “。此Unix时间戳可以通过使用 OS.get_datetime_from_unix_time .

  • String get_pascal_string ( )

返回A String 以Pascal格式保存。

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

将路径返回为 String 对于当前打开的文件。

  • String get_path_absolute ( ) const

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

  • int get_position ( ) const

返回文件光标的位置。

  • float get_real ( ) const

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

返回SHA-256 String 表示给定路径或空的文件 String 失败论。

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

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

  • bool is_open ( ) const

返回 true 如果文件当前已打开。

根据标志打开文件进行写入或读取。

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

以写入或读取模式打开加密文件。您需要传递一个二进制密钥来加密/解密它。

以写入或读取模式打开加密文件。您需要传递一个密码来加密/解密它。

  • void seek ( int position )

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

  • void seek_end ( int position=0 )

将文件读/写光标更改到指定位置(以文件结尾的字节为单位)。

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

  • void store_16 ( int value )

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

  • void store_32 ( int value )

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

  • void store_64 ( int value )

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

  • void store_8 ( int value )

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

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

存储给定的 PoolStringArray 以csv(逗号分隔值)格式在文件中格式化为一行。可以传递其他分隔符 delim 使用非默认值 "," (逗号)。此分隔符的长度必须为一个字符。

文本将被编码为UTF-8。

  • void store_double ( float value )

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

  • void store_float ( float value )

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

  • void store_line ( String line )

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

文本将被编码为UTF-8。

  • void store_pascal_string ( String string )

存储给定的 String 作为文件中的一行,采用pascal格式(即也存储字符串的长度)。

文本将被编码为UTF-8。

  • void store_real ( float value )

在文件中存储浮点数。

  • void store_string ( String string )

存储给定的 String 在文件中。

文本将被编码为UTF-8。

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

在文件中存储任何变量值。如果 full_objectstrue ,允许对对象进行编码(并且可能包括代码)。