Skip to content

Latest commit

 

History

History
583 lines (349 loc) · 38.2 KB

class_file.rst

File metadata and controls

583 lines (349 loc) · 38.2 KB
github_url:hide

File

Inherits: :ref:`Reference<class_Reference>` < :ref:`Object<class_Object>`

Type to handle file reading and writing operations.

Description

File type. This is used to permanently store data into the user device's file system and to read from it. This can be used to store game save data or player configuration files, for example.

Here's a sample on how to write and read from a file:

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

Tutorials

Properties

:ref:`bool<class_bool>` :ref:`endian_swap<class_File_property_endian_swap>` false

Methods

void :ref:`close<class_File_method_close>` ( )
:ref:`bool<class_bool>` :ref:`eof_reached<class_File_method_eof_reached>` ( ) const
:ref:`bool<class_bool>` :ref:`file_exists<class_File_method_file_exists>` ( :ref:`String<class_String>` path ) const
:ref:`int<class_int>` :ref:`get_16<class_File_method_get_16>` ( ) const
:ref:`int<class_int>` :ref:`get_32<class_File_method_get_32>` ( ) const
:ref:`int<class_int>` :ref:`get_64<class_File_method_get_64>` ( ) const
:ref:`int<class_int>` :ref:`get_8<class_File_method_get_8>` ( ) const
:ref:`String<class_String>` :ref:`get_as_text<class_File_method_get_as_text>` ( ) const
:ref:`PackedByteArray<class_PackedByteArray>` :ref:`get_buffer<class_File_method_get_buffer>` ( :ref:`int<class_int>` len ) const
:ref:`PackedStringArray<class_PackedStringArray>` :ref:`get_csv_line<class_File_method_get_csv_line>` ( :ref:`String<class_String>` delim="," ) const
:ref:`float<class_float>` :ref:`get_double<class_File_method_get_double>` ( ) const
:ref:`Error<enum_@GlobalScope_Error>` :ref:`get_error<class_File_method_get_error>` ( ) const
:ref:`float<class_float>` :ref:`get_float<class_File_method_get_float>` ( ) const
:ref:`int<class_int>` :ref:`get_len<class_File_method_get_len>` ( ) const
:ref:`String<class_String>` :ref:`get_line<class_File_method_get_line>` ( ) const
:ref:`String<class_String>` :ref:`get_md5<class_File_method_get_md5>` ( :ref:`String<class_String>` path ) const
:ref:`int<class_int>` :ref:`get_modified_time<class_File_method_get_modified_time>` ( :ref:`String<class_String>` file ) const
:ref:`String<class_String>` :ref:`get_pascal_string<class_File_method_get_pascal_string>` ( )
:ref:`String<class_String>` :ref:`get_path<class_File_method_get_path>` ( ) const
:ref:`String<class_String>` :ref:`get_path_absolute<class_File_method_get_path_absolute>` ( ) const
:ref:`int<class_int>` :ref:`get_position<class_File_method_get_position>` ( ) const
:ref:`float<class_float>` :ref:`get_real<class_File_method_get_real>` ( ) const
:ref:`String<class_String>` :ref:`get_sha256<class_File_method_get_sha256>` ( :ref:`String<class_String>` path ) const
:ref:`Variant<class_Variant>` :ref:`get_var<class_File_method_get_var>` ( :ref:`bool<class_bool>` allow_objects=false ) const
:ref:`bool<class_bool>` :ref:`is_open<class_File_method_is_open>` ( ) const
:ref:`Error<enum_@GlobalScope_Error>` :ref:`open<class_File_method_open>` ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` flags )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`open_compressed<class_File_method_open_compressed>` ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` mode_flags, :ref:`CompressionMode<enum_File_CompressionMode>` compression_mode=0 )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`open_encrypted<class_File_method_open_encrypted>` ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` mode_flags, :ref:`PackedByteArray<class_PackedByteArray>` key )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`open_encrypted_with_pass<class_File_method_open_encrypted_with_pass>` ( :ref:`String<class_String>` path, :ref:`ModeFlags<enum_File_ModeFlags>` mode_flags, :ref:`String<class_String>` pass )
void :ref:`seek<class_File_method_seek>` ( :ref:`int<class_int>` position )
void :ref:`seek_end<class_File_method_seek_end>` ( :ref:`int<class_int>` position=0 )
void :ref:`store_16<class_File_method_store_16>` ( :ref:`int<class_int>` value )
void :ref:`store_32<class_File_method_store_32>` ( :ref:`int<class_int>` value )
void :ref:`store_64<class_File_method_store_64>` ( :ref:`int<class_int>` value )
void :ref:`store_8<class_File_method_store_8>` ( :ref:`int<class_int>` value )
void :ref:`store_buffer<class_File_method_store_buffer>` ( :ref:`PackedByteArray<class_PackedByteArray>` buffer )
void :ref:`store_csv_line<class_File_method_store_csv_line>` ( :ref:`PackedStringArray<class_PackedStringArray>` values, :ref:`String<class_String>` delim="," )
void :ref:`store_double<class_File_method_store_double>` ( :ref:`float<class_float>` value )
void :ref:`store_float<class_File_method_store_float>` ( :ref:`float<class_float>` value )
void :ref:`store_line<class_File_method_store_line>` ( :ref:`String<class_String>` line )
void :ref:`store_pascal_string<class_File_method_store_pascal_string>` ( :ref:`String<class_String>` string )
void :ref:`store_real<class_File_method_store_real>` ( :ref:`float<class_float>` value )
void :ref:`store_string<class_File_method_store_string>` ( :ref:`String<class_String>` string )
void :ref:`store_var<class_File_method_store_var>` ( :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` full_objects=false )

Enumerations

enum ModeFlags:

  • READ = 1 --- Opens the file for read operations.
  • WRITE = 2 --- Opens the file for write operations. Create it if the file does not exist and truncate if it exists.
  • READ_WRITE = 3 --- Opens the file for read and write operations. Does not truncate the file.
  • WRITE_READ = 7 --- Opens the file for read and write operations. Create it if the file does not exist and truncate if it exists.

enum CompressionMode:

  • COMPRESSION_FASTLZ = 0 --- Uses the FastLZ compression method.
  • COMPRESSION_DEFLATE = 1 --- Uses the DEFLATE compression method.
  • COMPRESSION_ZSTD = 2 --- Uses the Zstandard compression method.
  • COMPRESSION_GZIP = 3 --- Uses the gzip compression method.

Property Descriptions

Default false
Setter set_endian_swap(value)
Getter get_endian_swap()

If true, the file's endianness is swapped. Use this if you're dealing with files written on big-endian machines.

Note: This is about the file format, not CPU type. This is always reset to false whenever you open the file.

Method Descriptions

  • void close ( )

Closes the currently opened file.


Returns true if the file cursor has read past the end of the file.

Note: This function will still return false while at the end of the file and only activates when reading past it. This can be confusing but it conforms to how low-level file access works in all operating systems. There is always :ref:`get_len<class_File_method_get_len>` and :ref:`get_position<class_File_method_get_position>` to implement a custom logic.


Returns true if the file exists in the given path.

Note: Many resources types are imported (e.g. textures or sound files), and that their source asset will not be included in the exported game, as only the imported version is used (in the res://.import folder). To check for the existence of such resources while taking into account the remapping to their imported location, use :ref:`ResourceLoader.exists<class_ResourceLoader_method_exists>`. Typically, using File.file_exists on an imported resource would work while you are developing in the editor (the source asset is present in res://, but fail when exported).


Returns the next 16 bits from the file as an integer.


Returns the next 32 bits from the file as an integer.


Returns the next 64 bits from the file as an integer.


Returns the next 8 bits from the file as an integer.


Returns the whole file as a :ref:`String<class_String>`.

Text is interpreted as being UTF-8 encoded.


Returns next len bytes of the file as a :ref:`PackedByteArray<class_PackedByteArray>`.


Returns the next value of the file in CSV (Comma-Separated Values) format. You can pass a different delimiter delim to use other than the default "," (comma). This delimiter must be one-character long.

Text is interpreted as being UTF-8 encoded.


Returns the next 64 bits from the file as a floating-point number.


Returns the last error that happened when trying to perform operations. Compare with the ERR_FILE_* constants from :ref:`Error<enum_@GlobalScope_Error>`.


Returns the next 32 bits from the file as a floating-point number.


Returns the size of the file in bytes.


Returns the next line of the file as a :ref:`String<class_String>`.

Text is interpreted as being UTF-8 encoded.


Returns an MD5 String representing the file at the given path or an empty :ref:`String<class_String>` on failure.


Returns the last time the file was modified in unix timestamp format or returns a :ref:`String<class_String>` "ERROR IN file". This unix timestamp can be converted to datetime by using :ref:`OS.get_datetime_from_unix_time<class_OS_method_get_datetime_from_unix_time>`.


Returns a :ref:`String<class_String>` saved in Pascal format from the file.

Text is interpreted as being UTF-8 encoded.


Returns the path as a :ref:`String<class_String>` for the current open file.


Returns the absolute path as a :ref:`String<class_String>` for the current open file.


Returns the file cursor's position.


Returns the next bits from the file as a floating-point number.


Returns a SHA-256 :ref:`String<class_String>` representing the file at the given path or an empty :ref:`String<class_String>` on failure.


Returns the next :ref:`Variant<class_Variant>` value from the file. If allow_objects is true, decoding objects is allowed.

Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution.


Returns true if the file is currently opened.


Opens the file for writing or reading, depending on the flags.


Opens a compressed file for reading or writing.


Opens an encrypted file in write or read mode. You need to pass a binary key to encrypt/decrypt it.


Opens an encrypted file in write or read mode. You need to pass a password to encrypt/decrypt it.


Changes the file reading/writing cursor to the specified position (in bytes from the beginning of the file).


Changes the file reading/writing cursor to the specified position (in bytes from the end of the file).

Note: This is an offset, so you should use negative numbers or the cursor will be at the end of the file.


Stores an integer as 16 bits in the file.


Stores an integer as 32 bits in the file.


Stores an integer as 64 bits in the file.


Stores an integer as 8 bits in the file.


Stores the given array of bytes in the file.


Store the given :ref:`PackedStringArray<class_PackedStringArray>` in the file as a line formatted in the CSV (Comma-Separated Values) format. You can pass a different delimiter delim to use other than the default "," (comma). This delimiter must be one-character long.

Text will be encoded as UTF-8.


Stores a floating-point number as 64 bits in the file.


Stores a floating-point number as 32 bits in the file.


Stores the given :ref:`String<class_String>` as a line in the file.

Text will be encoded as UTF-8.


Stores the given :ref:`String<class_String>` as a line in the file in Pascal format (i.e. also store the length of the string).

Text will be encoded as UTF-8.


Stores a floating-point number in the file.


Stores the given :ref:`String<class_String>` in the file.

Text will be encoded as UTF-8.


Stores any Variant value in the file. If full_objects is true, encoding objects is allowed (and can potentially include code).